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
A discord user recommended
const address = await req.json();
instead ofconst {address} = req.body
and it works.I think what you want to do is:
I’ve faced a similiar problem, you can try this: