-
-
Save parzibyte/7558078d8412a9b0ad86cd77e29328a9 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
enrutador.HandleFunc("/usuario", agregarUsuario).Methods("POST") | |
func agregarUsuario(respuesta http.ResponseWriter, peticion *http.Request) { | |
type Usuario struct { | |
Id int `json:"id"` | |
Correo string `json:"correo"` | |
} | |
var usuarioNuevo Usuario | |
// Intenta decodificar el cuerpo de la petición (peticion.Body) dentro de usuario (&usuario) | |
err := json.NewDecoder(peticion.Body).Decode(&usuarioNuevo) | |
if err != nil { | |
json.NewEncoder(respuesta).Encode("Cuerpo de petición no válido") | |
return | |
} | |
// Si el usuario era válido lo agregamos al arreglo | |
usuarios = append(usuarios, usuarioNuevo) | |
json.NewEncoder(respuesta).Encode(usuarioNuevo) | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment