Next.js Discord

Discord Forum

route.js POST request

Unanswered
Barbary Lion posted this in #help-forum
Open in Discord
Barbary LionOP
Hi i am sending a post request that contains username and password to the api route i created, but when console log the req to find the credentials i dont get them

5 Replies

@Barbary Lion Hi i am sending a post request that contains username and password to the api route i created, but when console log the req to find the credentials i dont get them
That’s not how you handle route handlers. Request body json should be consumed by req.json() https://nextjs.org/docs/app/building-your-application/routing/route-handlers#request-body

Route handlers do not follow the syntax of api routes (so it’s not (req, res) like api routes, your res.status… won’t work), please check the route handler documentation page and read it carefully. It’s not just moving from a default export to per-method named exports, it’s a lot more than that
Barbary LionOP
what about setHeader ?
@Barbary Lion what about setHeader ?
The follow the native response way,
export async function GET(request: Request) {
  return new Response('Hello, Next.js!', {
    status: 200,
    headers: {
      'Access-Control-Allow-Origin': '*',
      'Access-Control-Allow-Methods': 'GET, POST, PUT, DELETE, OPTIONS',
      'Access-Control-Allow-Headers': 'Content-Type, Authorization',
    },
  })
}
Barbary LionOP
dumb me, thank you very much @joulev