Thanks to visit codestin.com
Credit goes to github.com

Skip to content

Cookies don't send after redirect #437

@sheldhur

Description

@sheldhur
  const options = {
    follow_set_cookies: true,
    cookies: {
      test: new Date().valueOf()
    }
  };

  const response = await needle('GET', 'https://setcookie.net/', undefined, options);
  console.log(response.body.match(/<code>(.+)<\/code>/i)?.[1]);

  const response2 = await needle('GET', 'https://shorturl.at/BQUI3', undefined, options);
  console.log(response2.body.match(/<code>(.+)<\/code>/i)?.[1]);

The example can looks strange but in real case it's a redirect from www.host.com to host.com

It was broken in 3.1.0 after had added the challenge host_and_ports_match

needle/lib/needle.js

Lines 542 to 553 in 9454d7b

// if follow_set_cookies is true, insert cookies in the next request's headers.
// we set both the original request cookies plus any response cookies we might have received.
if (config.follow_set_cookies && utils.host_and_ports_match(headers.location, uri)) {
var request_cookies = cookies.read(config.headers['cookie']);
config.previous_resp_cookies = resp.cookies;
if (Object.keys(request_cookies).length || Object.keys(resp.cookies || {}).length) {
config.headers['cookie'] = cookies.write(extend(request_cookies, resp.cookies));
}
} else if (config.headers['cookie']) {
debug('Clearing original request cookie', config.headers['cookie']);
delete config.headers['cookie'];
}

#428

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions