Return Json to the client

0

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)
                }
            }
    
asked by Leandro Gutierrez 09.02.2018 в 15:22
source

1 answer

2

I share, if you could share more of your code, or the function ExistenciaPersona(dni.Dni) I made the following code, I think you're not returning the structure as such and that's why it does not convert to JSON, I share the code I checked and it works excellent, a recommendation can go consulting with fmt.Println (err) the outputs of errors and go discarding the error You pass the following JSON:

update , if it is necessary to have a structure to have a JSON output.

{
    "dnivalue": "123H3"
}

and return the next JSON:

{
    "respuesta": 1
}

Code:

package main

import (
    "encoding/json"
    "io"
    "net/http"

    "github.com/gorilla/mux"
)

func main() {
    r := mux.NewRouter()
    r.HandleFunc("/", PersonaHandler).Methods("POST")
    http.Handle("/", r)
    http.ListenAndServe(":3000", nil)
}

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
        err := json.NewDecoder(r.Body).Decode(&dni)
        if err == io.EOF || err != nil {
            http.Error(w, "Empty Body", 400)
            return
        }
        respuesta := ExistenciaPersona(dni.Dni)
        w.Header().Set("Content-Type", "application/json")
        w.WriteHeader(http.StatusCreated)
        json.NewEncoder(w).Encode(respuesta)
        if err != nil {
            http.Error(w, err.Error(), 409)
            return
        }
    }

}

func ExistenciaPersona(dni string) Response {
    var respuesta Response
    if dni == "123H3" {
        respuesta.Respuesta = 1
        return respuesta
    }
    respuesta.Respuesta = 0
    return respuesta
}
    
answered by 12.02.2018 в 04:43