I am encountering an issue with my server-side code where a value I am sending is not being interpreted correctly. My project is utilizing the experimental App directory feature of NextJS.
//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 });
}
}
The output from the console log is:
ReadableStream { locked: false, state: 'readable', supportsBYOB: false }
The value of const address is undefined
.
This is the API call being made:
const response = await fetch("/api/auth", {
method: "POST",
headers: {
"Content-Type": "application/json",
},
body: JSON.stringify({ address: walletAddress }),
});
const data = await response.json();
I came across another answer to a similar problem which mentioned that NextJS version 12 and above should automatically parse the request. Can anyone point out what might be going wrong in my implementation? Is there a specific protocol in place within NextJS for decoding ReadableStreams that I may have missed in the documentation or examples? Perhaps there is a framework-agnostic approach for decoding objects that I am unaware of?
Thank you for any assistance provided.