Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

User agent not setting on redirects #91

Open
anpez opened this issue Jul 27, 2016 · 5 comments
Open

User agent not setting on redirects #91

anpez opened this issue Jul 27, 2016 · 5 comments

Comments

@anpez
Copy link

anpez commented Jul 27, 2016

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
Copy link
Contributor

fraenky8 commented Jul 30, 2016

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 golang/go#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
Copy link
Author

anpez commented Aug 4, 2016

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

@parnurzeal
Copy link
Owner

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

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

@mcauto
Copy link

mcauto commented Nov 29, 2018

How to get just 301 status code instead of error?

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

No branches or pull requests

5 participants