I've written a little middleware to help catch instances where I write some 
content in an HTTP handler, but forget to set the Content-Type header. I 
prefer to always set it to avoid forcing the browser to guess. It feels 
good and produces more predictable results.

I've noticed some of my redirect responses being marked as not having 
Content-Type header.

This is because of the implementation of http.Redirect:

https://github.com/golang/go/blob/83fb9c8d9f5511f5aca2a0eb9f7507e2527a76a9/src/net/http/server.go#L1961-L2022

It includes the following snippet:

// RFC 2616 recommends that a short note "SHOULD" be included in the
// response because older user agents may not understand 301/307.
// Shouldn't send the response for POST or HEAD; that leaves GET.
if r.Method == "GET" {
note := "<a href=\"" + htmlEscape(urlStr) + "\">" + statusText[code] + 
"</a>.\n"
fmt.Fprintln(w, note)
}

So if the method is GET, it writes a note that looks like HTML... But it 
doesn't set a Content-Type.

Given my desire to be explicit about setting Content-Types, should I always 
use http.Redirect as follows:

if r.Method == "GET" {
w.Header().Set("Content-Type", "text/html; charset=utf-8")
}
http.Redirect(w, req, targetURL, http.StatusSeeOther)

Or is this something that http.Redirect should be doing itself?

Or is it fine not to set Content-Type header in the case of a redirect 
status code? If so, why is that?

Thanks.

-- 
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+unsubscr...@googlegroups.com.
For more options, visit https://groups.google.com/d/optout.

Reply via email to