Krash
Krash

Reputation: 2287

How to separately set error code along with marshalling json in response body?

In my application, when there is error I am writing a json error message on response body but this makes response code 200. I tried separately doing

json.NewEncoder(res).Encode(errorBody)
res.WriteHeader(http.StatusBadRequest)

But it still gives out response code 200 along with a warning that I am making multiple WriteHeader calls. I want to have something like

http.Error(res,"Some Error Message here",http.StatusBadRequest)

but instead of the error message in text format, I want it to be in JSON. What should I do?

Upvotes: 0

Views: 567

Answers (2)

mkopriva
mkopriva

Reputation: 38203

You can write the header first and then the body.

res.Header().Set("Content-Type", "application/json; charset=utf-8")
res.WriteHeader(http.StatusBadRequest)
if err = json.NewEncoder(res).Encode(err); err != nil {
    // handle encoder error
}

And if the response is still 200 and/or you're getting the warning, that means that you or some of the packages you use are writing the header for you in some other place.

Update: (to answer relevant question from comment)

json.NewEncoder(res).Encode(err) calls the Write method on res, and the implementation of Write from the standard net/http package automatically sets the status to 200 if it's not already set. And the implementation of WriteHeader checks whether status is already set or not and if it is, it just logs the warning and bails, without overwriting it.

So if you wan't to control the status you need to call WriteHeader before your write to the response whether you're writing json with the encoder or calling res.Write yourself.

Upvotes: 1

backing
backing

Reputation: 127

json format is still plain text. json.Marshal errorBody to string first.

body, err := json.Marshal(errBody)
if err != nil{
    // todo
}
http.Error(res, string(body), http.StatusBadRequest)

Upvotes: 0

Related Questions