parnurzeal / gorequest

GoRequest -- Simplified HTTP client ( inspired by nodejs SuperAgent )
http://parnurzeal.github.io/gorequest/
MIT License
3.43k stars 414 forks source link

User agent not setting on redirects #91

Open anpez opened 8 years ago

anpez commented 8 years ago

I am creating a new agent like so:

gorequest. New(). Get(url). Set("User-Agent", useragent). EndBytes()

When url returns 301 and the http client re-requests for the new location, the user agent is not being set.

fraenky8 commented 8 years ago

I think that has nothing to do with gorequest directly, cuz it's using the net/http package underneath. General speaking all headers get lost at a redirect, see https://github.com/golang/go/issues/4800 There is also mentioned, that this behaviour can be controlled by CheckRedirect-function.

edit

see example below

    request := gorequest.New()
    _, body, _ := request.Get(url).
      RedirectPolicy(func(req gorequest.Request, via []gorequest.Request) error {
        for attr, val := range via[0].Header {
            if _, ok := req.Header[attr]; !ok {
                req.Header[attr] = val
            }
        }
        return nil
    }).
    Set("User-Agent", useragent).
    End()

    log.Println(body)
anpez commented 8 years ago

Thanks! That works. It should be in the doc, I think.

parnurzeal commented 7 years ago

Sorry for discussing unrelated stuff here. But I don't know how to reach you, @fraenky8. Could you send an email to me (parnurzeal at gmail)? I would like to consult you about this GoRequest.

lvillani commented 6 years ago

I tried to reproduce the issue but this seems to no longer be an issue as of today, with Go 1.9.

mcauto commented 5 years ago

How to get just 301 status code instead of error?