Help, I created a POST API endpoint and after i POST data to it, the request is always empty
Unanswered
Himalayan posted this in #help-forum
HimalayanOP
I created a POST API endpoint like this:
I am checking inspector and I am seeing I sent a POST request with a payload.
But when I console log out the request, it is always empty.
What am I doing wrong?
export async function POST(request: Request) {
console.log("request", JSON.stringify(request));
return NextResponse.json({ request });
}I am checking inspector and I am seeing I sent a POST request with a payload.
But when I console log out the request, it is always empty.
request {}What am I doing wrong?
14 Replies
HimalayanOP
@Himalayan I created a POST API endpoint like this:
export async function POST(request: Request) {
console.log("request", JSON.stringify(request));
return NextResponse.json({ request });
}
I am checking inspector and I am seeing I sent a POST request with a payload.
But when I console log out the request, it is always empty.
request {}
What am I doing wrong?
If you just do console.log(request) without serialising to json then it will show a lot of stuff
If you want to get the body then use await request.json()
HimalayanOP
When I console log request, I only got back: {}
my request variable is empty
@Himalayan When I console log request, I only got back: {}
the response is empty because
request is not a plain object, it's a collection of methods that can't be serialised so when NextResponse.json tries to serialise it the result is an empty objectbut the console.log will show lots of stuff, as long as you don't add
JSON.stringify to that console.logHimalayanOP
Why is request.body is empty
@joulev If you want to get the body then use await request.json()
use this to get the body of the request
HimalayanOP
Oh
Ok will try thx