¿Cómo procesar correctamente una solicitud POST JSON en Go?
¡Hola a todos! Acabo de empezar a aprender Go y estoy escribiendo un servidor REST API simple. Me encontré con un problema al procesar JSON en solicitudes POST.
Tengo una estructura User:
```go
type User struct {
Name string
Age int
}
```
Y un handler:
```go
func createUser(w http.ResponseWriter, r *http.Request) {
if r.Method != http.MethodPost {
http.Error(w, "Method not allowed", http.StatusMethodNotAllowed)
return
}
var user User
err := json.NewDecoder(r.Body).Decode(&user)
if err != nil {
http.Error(w, err.Error(), http.StatusBadRequest)
return
}
fmt.Fprintf(w, "User: %+v", user)
}
```
Cuando envío una solicitud POST con curl:
```
curl -X POST -H "Content-Type: application/json" -d '{"name":"John","age":30}' http://localhost:8080/user
```
Obtengo un objeto vacío: `User: {Name: Age:0}`. ¿Qué estoy haciendo mal? ¿Por qué no se llenan los campos? Verifiqué que el cuerpo de la solicitud llega — si imprimo r.Body como string,