To make an HTTP request with a custom body in Go, you can use the built-in net/http
package. Here is an example of making a POST request with a JSON body:
main.go818 chars40 lines
In this example, the data
variable is a map of strings that we want to send as a JSON payload. We first marshal the data to create a JSON byte slice.
We then create a new http.Request
with the POST method and the payload as the body, set the Content-Type
header to application/json
, and send the request with an http.Client
. The response is then handled as desired.
gistlibby LogSnag