Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

http: superfluous response.WriteHeader call

Tags:

http

go

I have a HandleFunc, which in case of success, I want (apparently) to be able to

  • sent a 200 response
  • write an informative message in the response

However the following snippet (executed in the happy path)

    if fullRun {
        w.Write([]byte(successMsg))
        w.WriteHeader(http.StatusOK)
    }

is causing this message in my application logs

http: superfluous response.WriteHeader

why is that?

Is there another pattern/practice to be followed when trying to both return 200 as also a (text) message?

like image 836
pkaramol Avatar asked Sep 05 '25 03:09

pkaramol


1 Answers

From the documentation for WriteHeader:

If WriteHeader is not called explicitly, the first call to Write will trigger an implicit WriteHeader(http.StatusOK). Thus explicit calls to WriteHeader are mainly used to send error codes.

So your first write call already calls WriteHeader, and your second call is ignored.

Call WriteHeader before you write the body, or don't call it if all you're sending is StatusOK.

like image 93
Burak Serdar Avatar answered Sep 08 '25 13:09

Burak Serdar