如何在 GET 请求 golang 中发送 JSON 正文?
How to send JSON body in GET request golang?
documentation 中没有 http.Client.Get
的“正文”字段
HTTP 不支持使用 GET 请求发送正文。有关详细信息,请参阅 this Q&A。但是如果你真的想这样做,即使你知道这是错误的,你也可以这样做:
iKnowThisBodyShouldBeIgnored := strings.NewReader("text that won't mean anything")
req, err := http.NewRequest(http.MethodGet, "http://example.com/foo", iKnowThisBodyShouldBeIgnored)
if err != nil {
panic(err)
}
res, err := http.DefaultClient.Do(req)
不要在 GET 请求中发送正文:an explanation。
RFC 7231 says 以下内容:
A payload within a GET request message has no defined semantics;
sending a payload body on a GET request might cause some existing
implementations to reject the request.
如果必须,请不要使用 net/http.Get
,因为它只是一个方便的功能。
相反,更深入地构建一个合适的 http.Request
which then perform by calling the Do
method on an instance of http.Client
(the http.DefaultClient
应该就可以了)。
documentation 中没有 http.Client.Get
的“正文”字段HTTP 不支持使用 GET 请求发送正文。有关详细信息,请参阅 this Q&A。但是如果你真的想这样做,即使你知道这是错误的,你也可以这样做:
iKnowThisBodyShouldBeIgnored := strings.NewReader("text that won't mean anything")
req, err := http.NewRequest(http.MethodGet, "http://example.com/foo", iKnowThisBodyShouldBeIgnored)
if err != nil {
panic(err)
}
res, err := http.DefaultClient.Do(req)
不要在 GET 请求中发送正文:an explanation。
RFC 7231 says 以下内容:
A payload within a GET request message has no defined semantics; sending a payload body on a GET request might cause some existing implementations to reject the request.
如果必须,请不要使用
net/http.Get
,因为它只是一个方便的功能。
相反,更深入地构建一个合适的http.Request
which then perform by calling theDo
method on an instance ofhttp.Client
(thehttp.DefaultClient
应该就可以了)。