Go Http Post request with basic auth and formvalue

huangapple go评论91阅读模式
英文:

Go Http Post request with basic auth and formvalue

问题

我正在为Go语言中的一个API编写一个包装器。该API使用基本身份验证,然后POST请求需要PostForm值。我正在做类似于以下的操作:

func NewFoo(name string) string {
    client := &http.Client{}
    URL := HOST + "foo/"
    req, err := http.NewRequest("POST", URL, nil)
    v := url.Values{}
    v.Set("name", name)
    req.Form = v
    req.SetBasicAuth(EMAIL, PASSWORD)
    resp, err := client.Do(req)
    if err != nil {
        log.Fatal(err)
    }
    bodyText, err := ioutil.ReadAll(resp.Body)
    s := string(bodyText)
    return s
}

我之前有一个类似的GET请求,没有表单值,它可以正常工作。但是当我运行这段代码时,它告诉我需要提供"name"值(所以它没有获取到)。

有没有什么原因导致这段代码不起作用?

英文:

I am writing a wrapper for an API in go. The api uses basic auth and then POST request requires PostForm value. I'm doing something like this:

func NewFoo(name string) string {
    client := &http.Client{}
    URL := HOST + "foo/"
    req, err := http.NewRequest("POST", URL, nil)
    v := url.Values{}
    v.Set("name", name)
    req.Form = v
    req.SetBasicAuth(EMAIL, PASSWORD)
    resp, err := client.Do(req)
    if err != nil {
		log.Fatal(err)
	}
	bodyText, err := ioutil.ReadAll(resp.Body)
	s := string(bodyText)
	return s
}

I had a similar, GET request without the form value and it works. When I run it, it tells me that the "name" value is required. (so it's not getting it)

Is there any reason this does not work?

答案1

得分: 27

从http://golang.org/pkg/net/http/#Request

// Form包含解析的表单数据,包括URL字段的查询参数和POST或PUT表单数据。
// 只有在调用ParseForm之后才能使用此字段。
// HTTP客户端忽略Form并使用Body代替。
Form url.Values

你需要将你的url.Values传递给请求的body。

func NewFoo(name string) string {
    client := &http.Client{}
    URL := HOST + "foo/"
    v := url.Values{}
    v.Set("name", name)
    // 将值传递给请求的body
    req, err := http.NewRequest("POST", URL, strings.NewReader(v.Encode()))
    req.SetBasicAuth(EMAIL, PASSWORD)
    resp, err := client.Do(req)
    if err != nil {
        log.Fatal(err)
    }
    bodyText, err := ioutil.ReadAll(resp.Body)
    s := string(bodyText)
    return s
}
英文:

From http://golang.org/pkg/net/http/#Request

// Form contains the parsed form data, including both the URL
// field's query parameters and the POST or PUT form data.
// This field is only available after ParseForm is called.
// The HTTP client ignores Form and uses Body instead.
Form url.Values

You have to pass your url.Values to the request's body instead.

func NewFoo(name string) string {
	client := &http.Client{}
	URL := HOST + "foo/"
	v := url.Values{}
	v.Set("name", name)
	//pass the values to the request's body
	req, err := http.NewRequest("POST", URL, strings.NewReader(v.Encode()))
	req.SetBasicAuth(EMAIL, PASSWORD)
	resp, err := client.Do(req)
	if err != nil {
		log.Fatal(err)
	}
	bodyText, err := ioutil.ReadAll(resp.Body)
	s := string(bodyText)
	return s
}

huangapple
  • 本文由 发表于 2014年6月15日 22:20:57
  • 转载请务必保留本文链接:https://go.coder-hub.com/24230500.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定