skip to Main Content

I’m sending a value to my server side code that isn’t being read correctly. I’m using the NextJS experimental App directory

//src/app/api/auth/route.js

export async function POST(req, res) {
  console.log(req.body);
  const { address } = req.body;
  const isAuthenticated = await checkBalance(address, threshold);
  if (isAuthenticated) {
    return new Response("Authorized", { status: 200 });
  } else if (isAuthenticated == false) {
    return new Response("Unauthorized", { status: 401 });
  } else if (isAuthenticated == undefined) {
    return new Response("Error", { status: 500 });
  }
}

console log is: ReadableStream { locked: false, state: 'readable', supportsBYOB: false }

const address is undefined.

This is the api call:

const response = await fetch("/api/auth", {
          method: "POST",
          headers: {
            "Content-Type": "application/json",
          },
          body: JSON.stringify({ address: walletAddress }),
        });
        const data = await response.json();

I read in another answer to a similar question that nextjs12+ is supposed to parse the request automatically- what am I doing wrong? I’m assuming that nextjs has a protocol in place for decoding the ReadableStream but I can’t find anything in the docs or examples for this, maybe because there is a framework agnostic method for decoding the object that is unknown to me?

Thank you in advance.

3

Answers


  1. Chosen as BEST ANSWER

    A discord user recommended const address = await req.json(); instead of const {address} = req.body and it works.


  2. I think what you want to do is:

    ...
    const { address } = req.body
    const isAuthenticated = await checkBalance(address, threshold);
    ...
    
    Login or Signup to reply.
  3. I’ve faced a similiar problem, you can try this:

    const body = await req.json()
    const {address} = body
    // the rest of your code 
    
    Login or Signup to reply.
Please signup or login to give your own answer.
Back To Top
Search