发送POST请求?

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

Go send post request?

问题

我想用Go发送POST请求,使用curl的请求如下:

curl 'http://192.168.1.50:18088/' -d '{"inputs": [{"desc":"program","ind":"14","p":"program"}]}'

我用Go这样做:

jobCateUrl := "http://192.168.1.50:18088/"

data := url.Values{}
queryMap := map[string]string{"p": "program", "ind": "14", "desc": "program"}
q, _ := json.Marshal(queryMap)
data.Add("inputs", string(q))

client := &http.Client{}
r, _ := http.NewRequest("POST", jobCateUrl, strings.NewReader(data.Encode()))
r.Header.Add("Content-Type", "application/x-www-form-urlencoded")
r.Header.Add("Content-Length", strconv.Itoa(len(data.Encode())))

resp, _ := client.Do(r)
fmt.Println(resp)

但是我失败了,得到了500错误,这是怎么回事?

英文:

I want to send POST request with Go, the request with curl is as follow:

curl 'http://192.168.1.50:18088/' -d '{"inputs": [{"desc":"program","ind":"14","p":"program"}]}'

I do this with Go like this:

jobCateUrl := "http://192.168.1.50:18088/"

data := url.Values{}
queryMap := map[string]string{"p": "program", "ind": "14", "desc": "program"}
q, _ := json.Marshal(queryMap)
data.Add("inputs", string(q))

client := &http.Client{}
r, _ := http.NewRequest("POST", jobCateUrl, strings.NewReader(data.Encode()))
r.Header.Add("Content-Type", "application/x-www-form-urlencoded")
r.Header.Add("Content-Length", strconv.Itoa(len(data.Encode())))

resp, _ := client.Do(r)
fmt.Println(resp)

but I failed, got 500 error, what wrong with this?

答案1

得分: 3

请求体不一样:

在curl中,你发送的是{"inputs": [{"desc":"program","ind":"14","p":"program"}]}

在go中,你发送的是inputs=%7B%22desc%22%3A%22program%22%2C%22ind%22%3A%2214%22%2C%22p%22%3A%22program%22%7D,解码后为inputs={"desc":"program","ind":"14","p":"program"}

所以,你可能应该这样做:

type body struct {
    Inputs []input `json:"input"`
}

type input struct {
    Desc string `json:"desc"`
    Ind  string `json:"ind"`
    P    string `json:"p"`
}

然后创建一个`body`

b := body{
    Inputs: []input{
        {
            Desc: "program",
            Ind:  "14",
            P:    "program",
        },
    },
}

对其进行编码

q, err := json.Marshal(b)
if err != nil {
    panic(err)
}

你显然不应该使用panic这只是为了演示无论如何`string(q)`将得到`{"input":[{"desc":"program","ind":"14","p":"program"}]}`

[Playground](http://play.golang.org/p/uAKp18oiGq)上试一试。

<details>
<summary>英文:</summary>

The Request bodies are not the same:

In curl, you send `{&quot;inputs&quot;: [{&quot;desc&quot;:&quot;program&quot;,&quot;ind&quot;:&quot;14&quot;,&quot;p&quot;:&quot;program&quot;}]}`

In go, you send `inputs=%7B%22desc%22%3A%22program%22%2C%22ind%22%3A%2214%22%2C%22p%22%3A%22program%22%7D` which URLDecodes to `inputs={&quot;desc&quot;:&quot;program&quot;,&quot;ind&quot;:&quot;14&quot;,&quot;p&quot;:&quot;program&quot;}`.

So, what you should probably do is something like this:

    type body struct {
	    Inputs []input `json:&quot;input&quot;`
    }

    type input struct {
	    Desc string `json:&quot;desc&quot;`
	    Ind  string `json:&quot;ind&quot;`
	    P    string `json:&quot;p&quot;`
    }

Then create a `body`:

	b := body{
        Inputs: []input{
		    {
			    Desc: &quot;program&quot;,
			    Ind:  &quot;14&quot;,
			    P:    &quot;program&quot;},
    		},
	}

Encode that:

	q, err := json.Marshal(b)
    if err != nil {
	    panic(err)
	}

You should obviously not panic, this is just for demonstration. Anyways, a `string(q)` will get you `{&quot;input&quot;:[{&quot;desc&quot;:&quot;program&quot;,&quot;ind&quot;:&quot;14&quot;,&quot;p&quot;:&quot;program&quot;}]}`.

Try it on the [Playground](http://play.golang.org/p/uAKp18oiGq)

</details>



# 答案2
**得分**: 0

你不需要设置"Content-Length"相反我认为你需要设置"host"属性

<details>
<summary>英文:</summary>

You don&#39;t need to set the &quot;Content-Length&quot;, and instead i think you need to set the &quot;host&quot; attribute. 


</details>



huangapple
  • 本文由 发表于 2015年11月6日 15:37:50
  • 转载请务必保留本文链接:https://go.coder-hub.com/33561703.html
匿名

发表评论

匿名网友

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

确定