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.
...
...
...
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);
})();
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.
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.