Payload blank return when performing return to WEB application in Golang

Asked

Viewed 54 times

4

I am checking in the API if the user’s email is already registered and returns a warning message. Even performing the requests the payload returns empty.

Even debugging and knowing that the payload on the api side is with the information it returns empty.

A friend, recommended me to make an interface and to throw the information inside it, turning an interface inside another interface, but it didn’t work.

Thank you.

func CreateUsuario(responseWriter http.ResponseWriter, r *http.Request) {

      var util util.StructApp

      msg := []interface{}{}
      msg = append(msg, "Erro")

      util.RespondWithJSON(responseWriter, http.StatusCreated, msg)
}




func (appStruct *StructApp) RespondWithJSON(responseWriter http.ResponseWriter, code int, payload interface{}) {
        var responseSuccess response.ResponseSuccess
        responseSuccess.Records = payload
        lenPayload := reflect.ValueOf(payload)
        responseSuccess.Meta.RecordCount = 1
        responseSuccess.Meta.Limit = 1

        if lenPayload.Kind() == reflect.Slice {
            responseSuccess.Meta.Limit = lenPayload.Len()
            responseSuccess.Meta.RecordCount = lenPayload.Len()
        }

        response, _ := json.Marshal(responseSuccess)
        responseWriter.Header().Set("Content-Type", "application/json")
        responseWriter.WriteHeader(code)
        responseWriter.Write(response)
    }
{"meta":{"limit":1,"offset":0,"recordCount":1},"records":{}}
  • Hello Caio, could add the return?

  • Hello Gabriel, added the return

1 answer

2


I took your example and removed the struct and changed the status, I think it will help you!

//Ping representa o teste de ping
func Ping(responseWriter http.ResponseWriter, r *http.Request) {
    var t util.StructApp
    t.RespondWithJSON(responseWriter, http.StatusOK, "pong")
}

My return

{"meta":{"limit":1,"offset":0,"recordCount":1},"records":"pong"}

Browser other questions tagged

You are not signed in. Login or sign up in order to post.