I'm trying to pass a cookie (which was set in the browser (e. g. Edge V102.0.1245.44 or Firefox V89.0.2)) by an axios request to another server (which is on the same host = localhost). I still tried a lot of different settings, but none did work. The cookie seems to be existing in the express request (http://localhost:3000/request
) req.headers.cookie
.
See:
Make Axios send cookies in its requests automatically
node.js axios request with cookies
Set cookies for cross origin requests
How to set cookies when send a request in node ?
Here is an example what I tried:
Send request http://localhost:3000/request
, cookie is set before by http://localhost:3000/setcookie
:
const express = require('express')
import { Request, Response } from "express"
const axios = require('axios')
const dayjs = require('dayjs')
const app = express()
const port = 3000
app.get('/setcookie', (req: Request, res: Response) => {
const cookieName = 'cookie-name'
const cookieValue = dayjs().format('HH:mm:ss DD.MM.YYYY')
res.cookie(cookieName, cookieValue);
res.send(`Cookie successfully set: ${cookieName}=${cookieValue}`);
});
let content = `Hello world`
app.get('/', (req: Request, res: Response) => {
console.log(req.cookies)
console.log(req.headers.cookie)
res.send(content)
})
app.get('/request', async (req: Request, res: Response) => {
console.log(req.cookies)
console.log(req.headers.cookie)
let url = 'http://localhost:2999'
try {
const result = await axios.get(url, { withCredentials: true })
console.log(result.headers)
}
catch (error) {
console.log(error)
}
res.send(true)
})
app.listen(port, () => {
console.log(`Example app listening on port ${port}`)
})
Request is send to:
const express = require('express')
import { Request, Response } from "express"
import cors from 'cors'
const app = express()
app.use(cors({
credentials: true,
origin: "http://localhost",
allowedHeaders: [
'Content-Type',
'Authorization',
],
}));
const port = 2999
app.get('/', (req: Request, res: Response) => {
console.log(req.headers)
console.log(req.cookies)
res.send(true)
})
app.listen(port, () => {
console.log(`Example app listening on port ${port}`)
})
Do you need more information?
I'm trying to pass a cookie (which was set in the browser (e. g. Edge V102.0.1245.44 or Firefox V89.0.2)) by an axios request to another server (which is on the same host = localhost). I still tried a lot of different settings, but none did work. The cookie seems to be existing in the express request (http://localhost:3000/request
) req.headers.cookie
.
See:
Make Axios send cookies in its requests automatically
node.js axios request with cookies
Set cookies for cross origin requests
How to set cookies when send a request in node ?
Here is an example what I tried:
Send request http://localhost:3000/request
, cookie is set before by http://localhost:3000/setcookie
:
const express = require('express')
import { Request, Response } from "express"
const axios = require('axios')
const dayjs = require('dayjs')
const app = express()
const port = 3000
app.get('/setcookie', (req: Request, res: Response) => {
const cookieName = 'cookie-name'
const cookieValue = dayjs().format('HH:mm:ss DD.MM.YYYY')
res.cookie(cookieName, cookieValue);
res.send(`Cookie successfully set: ${cookieName}=${cookieValue}`);
});
let content = `Hello world`
app.get('/', (req: Request, res: Response) => {
console.log(req.cookies)
console.log(req.headers.cookie)
res.send(content)
})
app.get('/request', async (req: Request, res: Response) => {
console.log(req.cookies)
console.log(req.headers.cookie)
let url = 'http://localhost:2999'
try {
const result = await axios.get(url, { withCredentials: true })
console.log(result.headers)
}
catch (error) {
console.log(error)
}
res.send(true)
})
app.listen(port, () => {
console.log(`Example app listening on port ${port}`)
})
Request is send to:
const express = require('express')
import { Request, Response } from "express"
import cors from 'cors'
const app = express()
app.use(cors({
credentials: true,
origin: "http://localhost",
allowedHeaders: [
'Content-Type',
'Authorization',
],
}));
const port = 2999
app.get('/', (req: Request, res: Response) => {
console.log(req.headers)
console.log(req.cookies)
res.send(true)
})
app.listen(port, () => {
console.log(`Example app listening on port ${port}`)
})
Do you need more information?
Share Improve this question edited Jul 15, 2022 at 4:57 user2625247 asked Jul 14, 2022 at 13:50 user2625247user2625247 3912 gold badges5 silver badges16 bronze badges2 Answers
Reset to default 7There are two different HTTP clients involved here:
- The http://localhost:3000/setcookie request is made by your browser and the browser stores the cookie.
- The http://localhost:2999 request is made by your Node.js process, which knows nothing about the browser's cookies.
You would have to copy the cookies from the ining request into the outgoing (axios) request, perhaps so:
axios.get(url, {
headers: {cookie: req.headers.cookie}
})
Neither withCredentials: true
nor CORS settings are needed in this case, because both are relevant only if the HTTP client is a browser.
I know this has been answered already but for those like my former self who stumble upon this I remend adding 2 things:
- cors implementation with credentials and origin:
app.use(cors({ credentials: true, origin: 'http://localhost:xxxx' }));
- withCredentials to every request that involves the cookie (at least that's what worked for me). Here is my client side, where on the other end (server) I used a middleware to check for the JWT token in the cookie:
updateItem(id: string, updatedItem: ItemModel) {
// Update the item on the server
return this.http.put('http://localhost:xxxx/api/item/' + id, updatedItem,
{withCredentials: true}).pipe( //rest of code
and also my cookie:
res.cookie('authToken', token, {
domain: 'localhost',
httpOnly: true,/* secure: true, if using HTTPS */
secure: false,
sameSite: sameSite,
path: '/',
maxAge: 3600000, /* 1 hour */
});
when I check the Network tab the cookie is present but not when I log it: console.log(req.cookies) it's "[Object: null prototype] {}". I recently implemented this so this could perish as well but for the time being it works.