-9

There is no "body" field in documentation for http.Client.Get

kostix
  • 43,267
  • 10
  • 69
  • 137
  • 4
    GET 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. – Flimzy Jul 23 '20 at 15:15
  • 2
    Though, 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 Answers2

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)
Flimzy
  • 60,850
  • 13
  • 104
  • 147
3
  1. 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.

  2. If you must, do not use net/http.Get as it's just a convenience function.
    Instead, go one level deeper and construct a proper http.Request which then perform by calling the Do method on an instance of http.Client (the http.DefaultClient should be just fine).

kostix
  • 43,267
  • 10
  • 69
  • 137