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

Cookies are ignored on redirects #83

Open
Alyxsqrd opened this issue Oct 1, 2022 · 5 comments
Open

Cookies are ignored on redirects #83

Alyxsqrd opened this issue Oct 1, 2022 · 5 comments
Labels
bug Something isn't working good first issue Good for newcomers

Comments

@Alyxsqrd
Copy link

Alyxsqrd commented Oct 1, 2022

Hi, it appears that this library ignores the cookies provided by the Set-Cookie header in a 301/302 redirect.

My best guess as to what's causing this: when Dart's HTTP library has followRedirects = true, ONLY the headers from the final destination are returned, meaning the headers from the prior responses are totally lost. ☹️

Possible workaround could be:

  1. Use final request = Request(...) for all methods
  2. Set request.followRedirects = false
  3. Do var response = _handleHttpResponse(...) before return
  4. Check if response.statusCode is 301/302. If true: Set response = _httpRequest(HttpMethod.GET, response.headers['location'], ...)
  5. return response
@Alyxsqrd Alyxsqrd mentioned this issue Oct 1, 2022
9 tasks
@sehnryr
Copy link
Collaborator

sehnryr commented Oct 11, 2022

Hi @Alyxsqrd, I've tested locally on dart vm and flutter web your fix and requests 4.7.0 but I can't reproduce your issue. I'm scraping a website that does 302 redirections and the cookies are set. Maybe extractResponseCookies isn't working like it should've, but that would be weird.

static CookieJar extractResponseCookies(Map<String, String> responseHeaders) {

Or the regexes in CookieJar.parseCookiesString are not picking your cookie.
static CookieJar parseCookiesString(String cookiesString) {

Is your fix working for you ?

@sehnryr
Copy link
Collaborator

sehnryr commented Oct 11, 2022

Or maybe you thought that this package does redirects by itself ? Unlike Python's requests library, this requests dart package does not yet support redirects, this matter was discussed in #11 but was unfortunately left on hiatus.

@sehnryr
Copy link
Collaborator

sehnryr commented Oct 11, 2022

Can you test doing your request that does a 301/302 redirect and see the stored cookies with this: and tell me if the cookies are set or not ?

CookieJar cookieJar = await Requests.getStoredCookies(Requests.getHostname(url));
print(cookieJar.values);

@sehnryr sehnryr added the question Further information is requested label Oct 13, 2022
@bojeckkk
Copy link

bojeckkk commented Nov 6, 2022

I've experienced similar issues. Requests library doesn't even have a chance to save cookies from intermediate responses, as these are not even returned to Requests when followRedirects is set to true.

@sehnryr sehnryr added bug Something isn't working good first issue Good for newcomers and removed question Further information is requested labels Nov 8, 2022
@dominic-schmid
Copy link

dominic-schmid commented May 3, 2024

I'm running into this same issue right now. A POST request to authenticate is redirected but this library does not follow up on that redirect with the cookies intact, so the server believes me to be unauthenticated after the redirect.

Which workarounds would I have as of today?

Edit: I should add that, generally, a 302 after a POST request isn't right. But the site I am working with is doing that so it leaves me no choice. It would be great if I could somehow still follow this redirect

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
bug Something isn't working good first issue Good for newcomers
Projects
None yet
Development

Successfully merging a pull request may close this issue.

4 participants