How to send JSON string in POST request in Go (Golang)
Hello everyone! I just started learning Go and I'm trying to figure out how to send JSON data via POST request to an external REST API. I have a struct with user data that I want to send to the server in JSON format.
Here's my current code that doesn't work:
```go
package main
import (
"fmt"
"net/http"
"strings"
)
type User struct {
Name string `json:"name"`
Email string `json:"email"`
Age int `json:"age"`
}
func main() {
user := User{
Name: "Ivan Petrov",
Email: "ivan@example.com",
Age: 30,
}
// Trying to send JSON string
resp, err := http.Post(
"https://api.example.com/users",
"application/json",
strings.NewReader(user), // This doesn't work!
)