google / jsonapi

jsonapi.org style payload serializer and deserializer
http://godoc.org/github.com/google/jsonapi
MIT License
1.42k stars 211 forks source link

jsonapi.MarshalPayload sets response status to 200, but I want 201 (created) #218

Open gabbottron opened 1 year ago

gabbottron commented 1 year ago

I am using jsonapi with gin in a go project. In my create handlers I need to marshal the response and send it back, but the function sets the response code to 200 automagically and I want 201. Gin will not let me override the 200 response after this code block runs. Could you allow for a custom response code on this function if you insist on setting headers?

Thanks! -Geoff

crodwell commented 1 year ago

it's literally in the readme:

w.Header().Set("Content-Type", jsonapi.MediaType)
w.WriteHeader(http.StatusCreated)

if err := jsonapi.MarshalPayload(w, blog); err != nil {
    http.Error(w, err.Error(), http.StatusInternalServerError)
}