Next.js Discord

Discord Forum

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
Open in Discord
HimalayanOP
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?

14 Replies

HimalayanOP
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 object
but the console.log will show lots of stuff, as long as you don't add JSON.stringify to that console.log
HimalayanOP
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