TransWikia.com

How to send JSON body in GET request golang?

Stack Overflow Asked on December 13, 2021

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

2 Answers

  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).

Answered by kostix on December 13, 2021

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)

Answered by Flimzy on December 13, 2021

Add your own answers!

Ask a Question

Get help from others!

© 2024 TransWikia.com. All rights reserved. Sites we Love: PCI Database, UKBizDB, Menu Kuliner, Sharing RPP