Got: CookieJar not working with got.extend

Created on 12 Jun 2020  路  4Comments  路  Source: sindresorhus/got

Describe the bug

  • Node.js version: 14
  • OS & version:
this.cookieJar = new CookieJar();
this.client = got.extend({ cookieJar: this.cookieJar });

It will work on the first request but not read the cookie on the 2nd request. When explicitly setting this.cookieJar on the 2nd request, it does work.

Actual behavior

...

Expected behavior

...

Code to reproduce

...

Checklist

  • [x] I have read the documentation.
  • [x] I have tried my code with the latest version of Node.js and Got.

Most helpful comment

The issue was that one of the app's dependencies had a dependency on [email protected], which caused the error. [email protected] does work indeed.

Thanks for the help though.

All 4 comments

This code should be able to reproduce your issue

import got from 'got';
import express = require('express');
const cookieParser = require('cookie-parser');
const tough = require('tough-cookie');

const app = express();
app.use(cookieParser())
app.get('/', (req, res) => {
  const counter = req.cookies?.counter ? parseInt(req.cookies.counter) : 0;
  console.log('Server counter:', counter);
  res.cookie('counter', counter + 1).end();
});


(async () => {
  const server = app.listen(4444);

  const cookieJar = new tough.CookieJar();
  const client =  got.extend({ cookieJar: cookieJar });

  await client.get('http://127.0.0.1:4444');
  console.log(cookieJar.getCookieStringSync('http://127.0.0.1:4444'));
  await client.get('http://127.0.0.1:4444');
  console.log(cookieJar.getCookieStringSync('http://127.0.0.1:4444'));

  server.close();
})();

But it doesn't.
I'm testing it with the master branch from git (that currently is exactly the release 11.3).

If you're using the latest version a wild guess could be that in your software the client for the first request and the one for the second one are not the same.

const got = require("got");
const { CookieJar } = require("tough-cookie");

cookieJar = new CookieJar();
client = got.extend({ cookieJar });

(async () => {
  let response = await client.post("https://xyz", {
    headers: { "Upgrade-Insecure-Request": 12 },
    json: { ... },
  });

  response = await client("https://xyz");

  console.log(response.request.options.headers);
})();

[email protected]

Cookie not present in headers on 2nd request. It is, when explicitly setting cookieJar on 2nd request.

I really can't reproduce your issue, if you can't give a full example can you at least check if, after the first request, the response has the set-cookie header and the cookie jar has the cookie.

Like this:

const got = require("got");
const { CookieJar } = require("tough-cookie");

cookieJar = new CookieJar();
client = got.extend({ cookieJar });

(async () => {
  let response = await client.post("https://xyz", {
    headers: { "Upgrade-Insecure-Request": 12 },
    json: { ... },
  });

  console.log('Headers:', response.headers);
  console.log('Cookies in the jar:', cookieJar.getCookieStringSync('https://xyz'));
})();

The issue was that one of the app's dependencies had a dependency on [email protected], which caused the error. [email protected] does work indeed.

Thanks for the help though.

Was this page helpful?
0 / 5 - 0 ratings

Related issues

joolfe picture joolfe  路  3Comments

lukechu10 picture lukechu10  路  3Comments

darksabrefr picture darksabrefr  路  3Comments

quocnguyen picture quocnguyen  路  4Comments

dominusmars picture dominusmars  路  3Comments