About http: superfluous response.WriteHeader call

2,422 views
Skip to first unread message

洪嘉鴻

unread,
Nov 11, 2019, 12:25:20 AM11/11/19
to golang-nuts
Hello everyone:
       I'm using golang with Win10. Now I'm trying to write a web browser. The version of the golang which I am using is 1.12.9. Here is the problem that I'm encountering:
Here is the code. The code couldn't run with "The Go Playground", while it works with the "Visual Studio Code".
The results are what I want. However, there appears the message "http: superfluous response.WriteHeader call from main.login" with line 34 and line 37.
If I remove these two lines, the results won't serve the file. I have no idea how to avoid the warning.
Here are the HTML files.

Any help is appreciated.
Thank you very much!
Max

burak serdar

unread,
Nov 11, 2019, 12:42:13 AM11/11/19
to 洪嘉鴻, golang-nuts
w.Write([]byte(result))
http.ServeFile(w, r, "form-3-2.html")

The reason for that warning is these two lines. The first line writes
some data to the response, which also forces the response writer to
write HTTP response headers, which at this point are empty. Then,
ServeFile sets some headers, and then writes those headers again,
thinking this is the first time the response was written. But the
headers are already written to the response, so the headers set by the
ServeFile function is lost. That's the reason you get that warning.
You should not set headers after you start writing the body.

You seem to be trying to add a piece of text to an html file. Instead
of doing it as you do, try using templates.

>
> Any help is appreciated.
> Thank you very much!
> Max
>
> --
> You received this message because you are subscribed to the Google Groups "golang-nuts" group.
> To unsubscribe from this group and stop receiving emails from it, send an email to golang-nuts...@googlegroups.com.
> To view this discussion on the web visit https://groups.google.com/d/msgid/golang-nuts/5e7457cf-119b-4be5-adc8-a94317fc837f%40googlegroups.com.

洪嘉鴻

unread,
Nov 11, 2019, 12:54:44 AM11/11/19
to golang-nuts
I see.
Thank you very much!
Max


burak serdar於 2019年11月11日星期一 UTC+8下午1時42分13秒寫道:
> To unsubscribe from this group and stop receiving emails from it, send an email to golan...@googlegroups.com.
Reply all
Reply to author
Forward
0 new messages