pkaramol
pkaramol

Reputation: 19332

http: superfluous response.WriteHeader call

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

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?

Upvotes: 3

Views: 12107

Answers (1)

Burak Serdar
Burak Serdar

Reputation: 51567

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.

Upvotes: 12

Related Questions