Content-type of response is set to application/json but text/plain is received by client


(Taalhach) #1

In my golang server i am writing response like this
w.WriteHeader(http.StatusOK)
w.Header().Set(“content-Type”, “application/json”)
fmt.Println(w.Header().Get(“content-Type”))
w.Write(response)
fmt.Println(w.Header().Get(“content-Type”))

but when i receive response on client its content type is text/plain
what is happening ?


(Lutz Horn) #2

Note this comment of ResponseWriter:

Changing the header map after a call to WriteHeader (or Write) has no effect unless the modified headers are trailers.

Which is exactly what you do.

See also this example which demonstrates this.

To make your code work, reorder the calls:

w.Header().Set(“content-Type”, “application/json”)
w.WriteHeader(http.StatusOK)
w.Write(response)

(Taalhach) #3

thank you so much you saved me :stuck_out_tongue: