Thanks Matt, that corroborates with my understanding too.

I've sent a CL https://go-review.googlesource.com/c/50510 to fix it, it can 
be reviewed after the tree unfreezes.

> (perhaps only if it's not already set?)

It's good to think this through, but I don't think it's worth checking for 
that. http.Redirect is meant to be used to a fresh request, one that hasn't 
been dealt with. We can't do much if the user misuses it. E.g., imagine if 
they set a different content-type and write some text, then call 
http.Redirect. It won't be able to set the correct status code, since the 
status code gets written as soon as you write some bytes to the 
http.ResponseWriter.


On Saturday, July 22, 2017 at 11:27:43 PM UTC-4, Matt Harden wrote:
>
> That sounds like a bug. The whole reason for including a message is for 
> older user-agents that don't understand redirects (must be *really* old! 
> So the Content-Type should be set as if responding normally to a GET 
> request. I think Redirect should be setting Content-Type (perhaps only if 
> it's not already set?).
>
> On Tue, Jul 18, 2017 at 8:41 PM Dmitri Shuralyov <shur...@gmail.com 
> <javascript:>> wrote:
>
>> 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...@googlegroups.com <javascript:>.
>> For more options, visit https://groups.google.com/d/optout.
>>
>

-- 
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