gpt4 book ai didi

post - 如何通过POST发送参数

转载 作者:数据小太阳 更新时间:2023-10-29 03:33:13 26 4
gpt4 key购买 nike

资源接收参数

示例:http://example.com/show-data?json={"fob":"bar"}

在 GET 请求的情况下,一切正常且运行良好。

urlStr := "http://87.236.22.7:1488/test/show-data"
json := `"foo":"bar"`
r, _ := http.Get(urlStr+`?json=`+json)
println(r.Status)


200 OK

但是使用POST请求应该怎么办呢?

我试试

 urlStr := "http://87.236.22.7:1488/test/show-data"
json := `{"foo":"bar"}`
form := url.Values{}
form.Set("json", json)

println(form.Encode())
post, _ := http.PostForm(urlStr, form)

println(post.Status)



400 Bad Request

json parameter is missing

但这行不通。

最佳答案

有很多方法可以发布内容,但您可能想使用 PostForm:https://golang.org/pkg/net/http/#PostForm

您需要先设置一个 Values 对象,然后将其直接传入。请参阅文档中的示例代码:https://golang.org/pkg/net/url/#Values

将 json 塞入 Values 对象后,只需调用 PostForm() 即可启动它。

编辑:假设接收端想要一些编码为 application/x-www-form-urlencoded 的东西,这是可行的。如果接收端需要 application/json,我会输入第二个答案。

关于post - 如何通过POST发送参数,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/39288079/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com