Next.js Discord

Discord Forum

POST Request body

Answered
Alligator mississippiensis posted this in #help-forum
Open in Discord
Alligator mississippiensisOP
Undefined when trying to access request body.

import { NextRequest, NextResponse } from "next/server";

export async function POST(req, res) {
  const { body } = req;
  const { name, email, password } = body;
  console.log(name);

  if (!name || !email || !password) {
    return new NextResponse("Missing fields");
  }

  res.status(200);
}


req.body returns:
ReadableStream { locked: false, state: 'readable', supportsBYOB: false }


But the body object with name email and pass is missing out of it.
Answered by Alligator mississippiensis
I finally got it working. The "Content-Type" was not set to "application/json"
View full answer

8 Replies

Alligator mississippiensisOP
I wish it was that simple but nope.


If i do that i get:

SyntaxError: Unexpected token w in JSON at position 71
The body of the request is that readable stream object
if you used a formData then await req.formData()
if you used a json then as above
if you used <your own format> then use the corresponding function to parse the request
Alligator mississippiensisOP
I finally got it working. The "Content-Type" was not set to "application/json"
Answer