getting empty request object on vercel
Answered
European anchovy posted this in #help-forum
![Avatar](https://cdn.discordapp.com/embed/avatars/4.png)
European anchovyOP
export async function GET(request: NextRequest) {
try {
const token = request.cookies.get("token")?.value;
if (!token) {
return NextResponse.json({
success: false,
msg:
reqCookiesLog:request
});
}
const dataFromToken = await jwt.verify(token as string, "thisismytoken");
const user = await User.findOne({
email: (dataFromToken as JwtPayload).email,
});
if (!user) {
return NextResponse.json({ msg: "User not found OR invalid token" });
}
return NextResponse.json({
success: true,
msg: "get user successfully",
data: user,
});
} catch (error) {
console.log({ error });
return NextResponse.json({ success: false, msg:
}
}
i have a query can anyone pls help
i have deployed my nextjs app on vercel, In GET api route i m accessing token in cookies which is there in headers inside network tab while calling api but the issue is i am getting empty request body
in local it is working properly
Here is my code
try {
const token = request.cookies.get("token")?.value;
if (!token) {
return NextResponse.json({
success: false,
msg:
token is not available
,reqCookiesLog:request
});
}
const dataFromToken = await jwt.verify(token as string, "thisismytoken");
const user = await User.findOne({
email: (dataFromToken as JwtPayload).email,
});
if (!user) {
return NextResponse.json({ msg: "User not found OR invalid token" });
}
return NextResponse.json({
success: true,
msg: "get user successfully",
data: user,
});
} catch (error) {
console.log({ error });
return NextResponse.json({ success: false, msg:
ERROR ${error}
});}
}
i have a query can anyone pls help
i have deployed my nextjs app on vercel, In GET api route i m accessing token in cookies which is there in headers inside network tab while calling api but the issue is i am getting empty request body
in local it is working properly
Here is my code
Answered by European anchovy
@DirtyCajunRice | AppDir @European sprat thank u for your response , fixed my issue by adding this export const dynamic = 'force-dynamic' in route file
12 Replies
![Avatar](https://cdn.discordapp.com/avatars/184479429404262410/a_35db4fb943e84d39bb02d6ec0f1d0b7e.gif?size=256)
DirtyCajunRice | AppDir
console log all the cookies.
![Avatar](https://cdn.discordapp.com/embed/avatars/4.png)
European anchovyOP
i am getting request as empty object, working fine on my local
![Avatar](https://cdn.discordapp.com/avatars/184479429404262410/a_35db4fb943e84d39bb02d6ec0f1d0b7e.gif?size=256)
DirtyCajunRice | AppDir
if its an app dir route handler properly put into a route.js you literally cant get an empty object.
![Avatar](https://cdn.discordapp.com/embed/avatars/2.png)
European sprat
It shouldn't be cached (because you're using request) but maybe it's still be cached because of how you're trying to access the cookies rather than using cookies()
https://nextjs.org/docs/app/building-your-application/routing/route-handlers#opting-out-of-caching
https://nextjs.org/docs/app/building-your-application/routing/route-handlers#opting-out-of-caching
![Avatar](https://cdn.discordapp.com/embed/avatars/4.png)
European anchovyOP
Error: Dynamic server usage: cookies while using NextJs
getting the above error on using this
const cookieStore = cookies()
const token = cookieStore.get('token')?.value
getting the above error on using this
const cookieStore = cookies()
const token = cookieStore.get('token')?.value
![Avatar](https://cdn.discordapp.com/avatars/184479429404262410/a_35db4fb943e84d39bb02d6ec0f1d0b7e.gif?size=256)
DirtyCajunRice | AppDir
even cached the entire request object wouldnt be empty
![Avatar](https://cdn.discordapp.com/embed/avatars/4.png)
European anchovyOP
@DirtyCajunRice | AppDir @European sprat tried everything still getting empty request {} working fine in my local
i have this middleware too
import { NextResponse } from "next/server";
import { NextRequest } from "next/server";
// This function can be marked `async` if using `await` inside
export function middleware(request: NextRequest) {
const token = request.cookies.get("token")?.value;
console.log('middleware console ????? request.cookies', request.cookies);
if (!token) {
return NextResponse.redirect(new URL("/signup", request.url));
}
return null
}
// See "Matching Paths" below to learn more
export const config = {
matcher: ["/"],
};
inside middleware i m getting token but in GET request i am getting empty
![Avatar](https://cdn.discordapp.com/avatars/184479429404262410/a_35db4fb943e84d39bb02d6ec0f1d0b7e.gif?size=256)
DirtyCajunRice | AppDir
Make a minimum reproducible example on code sandbox
![Avatar](https://cdn.discordapp.com/embed/avatars/4.png)
European anchovyOP
@DirtyCajunRice | AppDir https://codesandbox.io/p/sandbox/upbeat-driscoll-4mvj2z?file=/app/page.tsx:15,20
is this valid
is this valid
![Avatar](https://cdn.discordapp.com/embed/avatars/4.png)
European anchovyOP
@DirtyCajunRice | AppDir @European sprat thank u for your response , fixed my issue by adding this export const dynamic = 'force-dynamic' in route file
Answer