Clive B.
—You don’t know how to send JSON data in the body of an HTTP request in Go. For example:
{ "description": "Buy cheese and bread for breakfast." }
The simplest way to send JSON data in an HTTP request is to use the http.Post
function from the http
package, which requires only a URL, a content type, and a request body.
To send a JSON via an HTTP POST request, use http.Post
and wrap your JSON string in a bytes.Buffer
.
package main import ( "bytes" "log" "net/http" ) func main() { // Define the JSON body as a string. body := `{"description": "Buy cheese and bread for breakfast."}` // Make the request. response, err := http.Post("https://example.com/endpoint", "application/json", bytes.NewBufferString(body)) if err != nil { log.Fatalf("POST failed: %s", err) } // Do something with the response. log.Printf("POST returned with %d status", response.StatusCode) }
Note: The http.Post
function requires only a URL, a content type, and a request body. If you want more control over your request, check out http.Client
to learn how to set custom headers, timeouts, etc.
If you’d like to generate your JSON programmatically, use Go’s encoding/json
package.
The json.Marshal
function uses user-defined json
struct tags to format its output.
The Go Wiki offers more information about well-known struct tags.
package main import ( "bytes" "encoding/json" "log" "net/http" ) // Task contains the fields that you'd like to marshal (or serialise) as JSON. type Task struct { Title string `json:"title"` Description string `json:"description"` } func main() { // Define a Task instance. task := Task{ Title: "Morning task", Description: "Buy cheese and bread for breakfast.", } // Generate a byte slice representing the Task. body, err := json.Marshal(&task) if err != nil { log.Fatalf("Failed to marshal JSON: %s", err) } // Make the request. // Note that this time, `body` is a slice of bytes, so we use `bytes.NewBuffer` instead. response, err := http.Post("https://example.com/endpoint", "application/json", bytes.NewBuffer(body)) if err != nil { log.Fatalf("POST failed: %s", err) } // Do something with the response. log.Printf("POST returned with %d status", response.StatusCode) }
http.Post
function documentationhttp.Response
type documentationjson
package documentationTasty treats for web developers brought to you by Sentry. Get tips and tricks from Wes Bos and Scott Tolinski.
SEE EPISODESConsidered “not bad” by 4 million developers and more than 100,000 organizations worldwide, Sentry provides code-level observability to many of the world’s best-known companies like Disney, Peloton, Cloudflare, Eventbrite, Slack, Supercell, and Rockstar Games. Each month we process billions of exceptions from the most popular products on the internet.
Here’s a quick look at how Sentry handles your personal information (PII).
×We collect PII about people browsing our website, users of the Sentry service, prospective customers, and people who otherwise interact with us.
What if my PII is included in data sent to Sentry by a Sentry customer (e.g., someone using Sentry to monitor their app)? In this case you have to contact the Sentry customer (e.g., the maker of the app). We do not control the data that is sent to us through the Sentry service for the purposes of application monitoring.
Am I included?We may disclose your PII to the following type of recipients:
You may have the following rights related to your PII:
If you have any questions or concerns about your privacy at Sentry, please email us at compliance@sentry.io.
If you are a California resident, see our Supplemental notice.