Is there a way to set custom status code when using http.ServeFile()?


(t嘎) #1

If I do

res.WriteHeader(404)
http.ServeFile(res, req, notFoundHtmlFile)

it’ll give warning

http: superfluous response.WriteHeader call

also it’ll lose all the headers set by http.ServeFile like Content-Type


(Yamil Bracho) #2

I think you would need to create your own ServerFile, because it already returns HTTP OK (200). Look, your are returning a custom error page, and it is perfectly good that return a OK because the file will be returned.
I guess the process is read the file, and then write this string in ResponseWriter and setting the headers you would need


(t嘎) #3

Thanks. I guess that’s the way to go.

I want to use ServeFile because it does all the mime type guessing and other headers, and even have a nice interface for serving directories.