Avatar ·

How to send JSON string in POST request in Go?

Hello everyone! I recently started learning Go and am writing a small REST client to work with an external API. I need to send a POST request with a JSON body, but I can't figure out how to do it correctly. I have a data structure that I serialize to JSON, and I want to send it to the server. Here's what I tried: ```go package main import ( "fmt" "net/http" "strings" ) type User struct { Name string `json:"name"` Email string `json:"email"` } func main() { user := User{Name: "Ivan", Email: "ivan@example.com"} // How do I send this as JSON in a POST request? resp, err := http.Post("https://api.example.com/users", "application/json", strings.NewReader(user)) if err != nil { fmt.Println(err) } fmt.Print