There is no "body" field in documentation for http.Client.Get
Asked
Active
Viewed 2,219 times
-8
-
4GET and DELETE requests do not have body. If you need to send a body, use PUT, POST, PATCH – Burak Serdar Jul 23 '20 at 15:07
-
1@Flimzy, I'm inclined to think the linked answer, while definitely useful, does not answer the essense of the question: broken services (usually software developed in house) do exist, and I'd not be too surprised to find out the OP really needs to send a body in a GET request because it's what their target service _wants._ I would reopen the question. – kostix Jul 23 '20 at 15:12
-
@kostix: Fair point. – Jonathan Hall Jul 23 '20 at 15:15
-
2Though, if the problem is that there's an in-house server using broken standards, then creating an in-house client using broken standards means there are now two problems. Fixing the server would be the better long-term approach. – David Jul 23 '20 at 15:17
2 Answers
6
Sending a body with a GET request is not supported by HTTP. See this Q&A for full details. But if you really want to do this, even though you know it's wrong, you can do it this way:
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)

Jonathan Hall
- 75,165
- 16
- 143
- 189
3
Do not send body in a GET request: an explanation.
RFC 7231 says the following:
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.
If you must, do not use
net/http.Get
as it's just a convenience function.
Instead, go one level deeper and construct a properhttp.Request
which then perform by calling theDo
method on an instance ofhttp.Client
(thehttp.DefaultClient
should be just fine).