I explain my code a bit:
I have a form that worked under PHP and to learn what is happening to GO, it turns out that I want to verify if a person is already registered, with which I ask for your ID and launch the action "ExistencePersona". I decode the client's JSON and send that DNI to a function that searches the DB, then I receive a number, a 1 or a 0, depending on whether it exists or not. However when I do Encode I only receive a {}, not a number. I affirm that "answer" has the content {0} or {1}. In the same way I do not like this to make a structure just to keep a data.
How can I solve this so that the answer is the desired one? Can you send a JSON without having to build a structure? How would you respond when you only have to send a number?
type DNI struct {
Dni string 'json:"dnivalue"'
}
type Response struct {
respuesta int 'json:"respuesta"'
}
func PersonaHandler(w http.ResponseWriter, r *http.Request) {
param := r.URL.Query().Get("accion")
if param == "ExistenciaPersona" {
var dni DNI
if r.Body == nil {
http.Error(w, "Please send a request body", 400)
return
}
err := json.NewDecoder(r.Body).Decode(&dni)
if err != nil {
http.Error(w, err.Error(), 409)
return
} else {
respuesta := ExistenciaPersona(dni.Dni)
w.Header().Set("Content-Type", "application/json")
w.WriteHeader(http.StatusCreated)
json.NewEncoder(w).Encode(respuesta)
}
}