How can i get the cokies in an api route?
Unanswered
Arboreal ant posted this in #help-forum
Arboreal antOP
I tried doing
cookies().getAll() but i kept getting errors15 Replies
Arboreal antOP
Bump
in an api you could possibly use the following code to fetch all the cookies of a request.
request.cookiesfull examples
nextjs app router:
nextjs page router:
nextjs app router:
import { type NextRequest } from 'next/server'
export async function GET(request: NextRequest) {
console.log(request.cookies);
}nextjs page router:
import type { NextApiRequest, NextApiResponse } from 'next'
type ResponseData = {
message: string
}
export default function handler(
req: NextApiRequest,
res: NextApiResponse<ResponseData>
) {
console.log(req.cookies);
res.status(200).json({ message: 'Hello from Next.js!' })
}@Pearls full examples
nextjs app router:
tsx
import { type NextRequest } from 'next/server'
export async function GET(request: NextRequest) {
console.log(request.cookies);
}
nextjs page router:
tsx
import type { NextApiRequest, NextApiResponse } from 'next'
type ResponseData = {
message: string
}
export default function handler(
req: NextApiRequest,
res: NextApiResponse<ResponseData>
) {
console.log(req.cookies);
res.status(200).json({ message: 'Hello from Next.js!' })
}
Cuban Crocodile
this is not working any idea as of why?
@Cuban Crocodile this is not working any idea as of why?
Arboreal antOP
Still haven't tried it, did you get an error or?
i keep getting an error of undefined
@Pearls full examples
nextjs app router:
tsx
import { type NextRequest } from 'next/server'
export async function GET(request: NextRequest) {
console.log(request.cookies);
}
nextjs page router:
tsx
import type { NextApiRequest, NextApiResponse } from 'next'
type ResponseData = {
message: string
}
export default function handler(
req: NextApiRequest,
res: NextApiResponse<ResponseData>
) {
console.log(req.cookies);
res.status(200).json({ message: 'Hello from Next.js!' })
}
Arboreal antOP
how can i also send cokies? i recently switched my backend from express to nextjs
@Pearls full examples
nextjs app router:
tsx
import { type NextRequest } from 'next/server'
export async function GET(request: NextRequest) {
console.log(request.cookies);
}
nextjs page router:
tsx
import type { NextApiRequest, NextApiResponse } from 'next'
type ResponseData = {
message: string
}
export default function handler(
req: NextApiRequest,
res: NextApiResponse<ResponseData>
) {
console.log(req.cookies);
res.status(200).json({ message: 'Hello from Next.js!' })
}
Arboreal antOP
i got
RequestCookies {
_parsed: Map(0) {},
_headers: HeadersList {
cookies: null,
[Symbol(headers map)]: Map(9) {
'accept' => [Object],
'accept-encoding' => [Object],
'connection' => [Object],
'host' => [Object],
'user-agent' => [Object],
'x-forwarded-for' => [Object],
'x-forwarded-host' => [Object],
'x-forwarded-port' => [Object],
'x-forwarded-proto' => [Object]
},
[Symbol(headers map sorted)]: [
[Array], [Array],
[Array], [Array],
[Array], [Array],
[Array], [Array],
[Array]
]
}
} even tho i have a few cokies its still nulli messed around with an api a bit, and the following works fine for me:
import { NextResponse, type NextRequest } from 'next/server'
export async function GET(request: NextRequest) {
const cookies = request.cookies;
cookies.set('test', 'some-value');
cookies.set('test2', 'gonna_be_deleted');
cookies.delete('test2');
const allCookies = cookies.getAll();
return NextResponse.json({ allCookies });
}@Arboreal ant @Cuban Crocodile , the code above should work (it does for me)
@Pearls i messed around with an api a bit, and the following works fine for me:
tsx
import { NextResponse, type NextRequest } from 'next/server'
export async function GET(request: NextRequest) {
const cookies = request.cookies;
cookies.set('test', 'some-value');
cookies.set('test2', 'gonna_be_deleted');
cookies.delete('test2');
const allCookies = cookies.getAll();
return NextResponse.json({ allCookies });
}
Arboreal antOP
i got TypeError: next_headersWEBPACK_IMPORTED_MODULE_3.cookies.set is not a function, i also in your code you arent importing anything, i have to have
import { cookies } from 'next/headers'; otherwise i ge an error saying cookies is not defined@Pearls i messed around with an api a bit, and the following works fine for me:
tsx
import { NextResponse, type NextRequest } from 'next/server'
export async function GET(request: NextRequest) {
const cookies = request.cookies;
cookies.set('test', 'some-value');
cookies.set('test2', 'gonna_be_deleted');
cookies.delete('test2');
const allCookies = cookies.getAll();
return NextResponse.json({ allCookies });
}
Arboreal antOP
so i used your exact code, and it got all of the cokies correctlly. but it doesn't add the new ones, it sends the new ones in the response but it doesnt actually add them
Cuban Crocodile
so I have this and still no cookies showing, even though I have 3 of them from next-auth....