英文:
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 `{"inputs": [{"desc":"program","ind":"14","p":"program"}]}`
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={"desc":"program","ind":"14","p":"program"}`.
So, what you should probably do is something like this:
type body struct {
Inputs []input `json:"input"`
}
type input struct {
Desc string `json:"desc"`
Ind string `json:"ind"`
P string `json:"p"`
}
Then create a `body`:
b := body{
Inputs: []input{
{
Desc: "program",
Ind: "14",
P: "program"},
},
}
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 `{"input":[{"desc":"program","ind":"14","p":"program"}]}`.
Try it on the [Playground](http://play.golang.org/p/uAKp18oiGq)
</details>
# 答案2
**得分**: 0
你不需要设置"Content-Length",相反我认为你需要设置"host"属性。
<details>
<summary>英文:</summary>
You don't need to set the "Content-Length", and instead i think you need to set the "host" attribute.
</details>
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论