old row : req.Header = make(Header)
new row : req.Header = ireq.Header
在 2012年3月31日星期六UTC+8下午8时44分13秒,[email protected]写道:
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
--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 [email protected].
For more options, visit https://groups.google.com/groups/opt_out.