FAQ
For reference, this is occurring in net/http/client.go, lines 255 - 279,
when a new request is created and just its URL and Method copied.
To maintain the original header values (which is really quite useful, and I
can't think of any drawbacks), simply store a reference to the original
headers
after line 255 and change line 261 to:

req.Header = oldheader
On Saturday, 31 March 2012 13:44:13 UTC+1, stephane....@gmail.com wrote:

Hi again,
I'm quite stressing the net/http package ;)

I have discovered that if I set a custom HTTP header to a request, and
hit a 301/302/etc. redirect, those headers are not kept. Example code:

package main
import (
"log"
"net/http"
)
func main() {
client := &http.Client{}

req, err := http.NewRequest("GET", "http://combo.cc/redirect", nil)
if err != nil {
log.Fatalln(err)
}

req.Header.Set("User-Agent", "MSIE 5.15 (MacintoshOS 9)")
log.Println("User-Agent before redirect:", req.UserAgent())

resp, err := client.Do(req)
if err != nil {
log.Fatalln(err)
}
defer resp.Body.Close()

log.Println("User-Agent after redirect:", resp.Request.UserAgent())
}

Output:

2012/03/31 14:43:02 User-Agent before redirect: MSIE 5.15 (MacintoshOS 9)
2012/03/31 14:43:02 User-Agent after redirect:

This is what I'm getting on my nginx access.log:

93.56.194.199 - - [31/Mar/2012:14:36:59 +0200] "GET /redirect HTTP/1.1"
301 185 "-" "MSIE 5.15 (MacintoshOS 9)"
93.56.194.199 - - [31/Mar/2012:14:36:59 +0200] "GET / HTTP/1.1" 200 798 "
http://combo.cc/redirect" "Go http package"

As you can see the first request had my custom header, the second one
did not.

In http://golang.org/src/pkg/net/http/client.go, line 204, upon
redirect a new set of headers is being constructed, and only "Referer" is
set, instread of copying the headers from the original request.

If this looks like a bug to you, I'll open a report.

Stephane
--
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+unsubscribe@googlegroups.com.
For more options, visit https://groups.google.com/groups/opt_out.

Search Discussions

  • Dave Cheney at Sep 17, 2013 at 1:39 am
    Jamie, is there an issue logged for this ? The thread of discussion is
    over a year old now.

    On Tue, Sep 17, 2013 at 6:39 AM, Jamie Hall
    wrote:
    For reference, this is occurring in net/http/client.go, lines 255 - 279,
    when a new request is created and just its URL and Method copied.
    To maintain the original header values (which is really quite useful, and I
    can't think of any drawbacks), simply store a reference to the original
    headers
    after line 255 and change line 261 to:

    req.Header = oldheader

    On Saturday, 31 March 2012 13:44:13 UTC+1, stephane....@gmail.com wrote:

    Hi again,
    I'm quite stressing the net/http package ;)

    I have discovered that if I set a custom HTTP header to a request, and
    hit a 301/302/etc. redirect, those headers are not kept. Example code:

    package main
    import (
    "log"
    "net/http"
    )
    func main() {
    client := &http.Client{}

    req, err := http.NewRequest("GET", "http://combo.cc/redirect", nil)
    if err != nil {
    log.Fatalln(err)
    }

    req.Header.Set("User-Agent", "MSIE 5.15 (MacintoshOS 9)")
    log.Println("User-Agent before redirect:", req.UserAgent())

    resp, err := client.Do(req)
    if err != nil {
    log.Fatalln(err)
    }
    defer resp.Body.Close()

    log.Println("User-Agent after redirect:", resp.Request.UserAgent())
    }

    Output:

    2012/03/31 14:43:02 User-Agent before redirect: MSIE 5.15 (MacintoshOS 9)
    2012/03/31 14:43:02 User-Agent after redirect:

    This is what I'm getting on my nginx access.log:

    93.56.194.199 - - [31/Mar/2012:14:36:59 +0200] "GET /redirect HTTP/1.1"
    301 185 "-" "MSIE 5.15 (MacintoshOS 9)"
    93.56.194.199 - - [31/Mar/2012:14:36:59 +0200] "GET / HTTP/1.1" 200 798
    "http://combo.cc/redirect" "Go http package"

    As you can see the first request had my custom header, the second one
    did not.

    In http://golang.org/src/pkg/net/http/client.go, line 204, upon redirect
    a new set of headers is being constructed, and only "Referer" is set,
    instread of copying the headers from the original request.

    If this looks like a bug to you, I'll open a report.

    Stephane
    --
    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+unsubscribe@googlegroups.com.
    For more options, visit https://groups.google.com/groups/opt_out.
    --
    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+unsubscribe@googlegroups.com.
    For more options, visit https://groups.google.com/groups/opt_out.
  • Jamie Hall at Sep 17, 2013 at 8:45 am
    Sorry, I didn't spot the date until after I'd posted. Yes, the following
    issue relates to this for future reference:

    Issue 4800<https://code.google.com/p/go/issues/detail?id=4800&q=request%20header>
    On Saturday, 31 March 2012 13:44:13 UTC+1, stephane....@gmail.com wrote:

    Hi again,
    I'm quite stressing the net/http package ;)

    I have discovered that if I set a custom HTTP header to a request, and
    hit a 301/302/etc. redirect, those headers are not kept. Example code:

    package main
    import (
    "log"
    "net/http"
    )
    func main() {
    client := &http.Client{}

    req, err := http.NewRequest("GET", "http://combo.cc/redirect", nil)
    if err != nil {
    log.Fatalln(err)
    }

    req.Header.Set("User-Agent", "MSIE 5.15 (MacintoshOS 9)")
    log.Println("User-Agent before redirect:", req.UserAgent())

    resp, err := client.Do(req)
    if err != nil {
    log.Fatalln(err)
    }
    defer resp.Body.Close()

    log.Println("User-Agent after redirect:", resp.Request.UserAgent())
    }

    Output:

    2012/03/31 14:43:02 User-Agent before redirect: MSIE 5.15 (MacintoshOS 9)
    2012/03/31 14:43:02 User-Agent after redirect:

    This is what I'm getting on my nginx access.log:

    93.56.194.199 - - [31/Mar/2012:14:36:59 +0200] "GET /redirect HTTP/1.1"
    301 185 "-" "MSIE 5.15 (MacintoshOS 9)"
    93.56.194.199 - - [31/Mar/2012:14:36:59 +0200] "GET / HTTP/1.1" 200 798 "
    http://combo.cc/redirect" "Go http package"

    As you can see the first request had my custom header, the second one
    did not.

    In http://golang.org/src/pkg/net/http/client.go, line 204, upon
    redirect a new set of headers is being constructed, and only "Referer" is
    set, instread of copying the headers from the original request.

    If this looks like a bug to you, I'll open a report.

    Stephane
    --
    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+unsubscribe@googlegroups.com.
    For more options, visit https://groups.google.com/groups/opt_out.

Related Discussions

Discussion Navigation
viewthread | post
Discussion Overview
groupgolang-nuts @
categoriesgo
postedSep 16, '13 at 8:39p
activeSep 17, '13 at 8:45a
posts3
users2
websitegolang.org

2 users in discussion

Jamie Hall: 2 posts Dave Cheney: 1 post

People

Translate

site design / logo © 2021 Grokbase