N
N
NubasLol2019-11-19 14:01:33
go
NubasLol, 2019-11-19 14:01:33

How to add field to json?

How can I add the "status" field to "data" ?

func (r JsonResponse) SuccessResponse(w http.ResponseWriter, body interface{}) {
  data, err := json.Marshal(body)
}

Answer the question

In order to leave comments, you need to log in

1 answer(s)
P
Papa, 2019-11-19
@NubasLol

data, err := json.Marshal(body)
if err != nil {
    log.Println(err)
}

w.Header().Add("Content-Type", "application/json")
w.WriteHeader(http.StatusOK)
_, _ = w.Write(data)

But this code will not add a field, but will only send a http header with status 200 - OK.

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question