Next.js Discord

Discord Forum

NEXT.JS API using route.js accepting request body in POSTMAN but is doing so internally in next.js.

Answered
Transvaal lion posted this in #help-forum
Open in Discord
Transvaal lionOP
Can anyone offer an explanation or a solution to why this is happening?

Whenever i try to make a request to the app router's api using POSTMAN, i seem to get an error stating "Unexpected number in JSON at position 1" within the nextjs console and a 500 error.

it seems the issue lies within this section "const { shared_list_id, resource_id } = json" since when i remove it, it returns a 200 status code response.

Here is my the route method

export async function POST(req: Request) {
try {
// const currentUser = await getCurrentUserAction("db");

// if (!currentUser || !currentUser.id) {
// return new Response("Unauthorized", { status: 401 });
// }

const json = await req.json();

const { shared_list_id, resource_id } = json;

const alreadyInSharedResource = await findSharedResourceAction(
shared_list_id,
resource_id
);

if (alreadyInSharedResource) {
return new Response("Already in watchlist", { status: 409 });
}

console.log(shared_list_id, resource_id);

const createSharedResource = await createSharedResourceAction({
resource_id: resource_id,
shared_list_id: shared_list_id,
});

console.log(createSharedResource);

if (!createSharedResource) {
return new Response("Something went wrong", { status: 500 });
}

return new Response("Shared list created", { status: 200 });
} catch (error) {
console.log(error);
return new Response("Something went wrong", { status: 500 });
}
}
Answered by riský
like this i believe: https://apidog.com/articles/how-to-send-json-post-reques-with-postman/ (lol why does top google results have to be ads 😭 )
View full answer

17 Replies

when using postman, are you sending json body? or any body at all?
Transvaal lionOP
i tried using all the methods of sending
form-data
x-www
raw
send it with body with json data and json headers
Transvaal lionOP
ok, let me try real quick.
like this i believe: https://apidog.com/articles/how-to-send-json-post-reques-with-postman/ (lol why does top google results have to be ads 😭 )
Answer
Transvaal lionOP
checking it out.
Its working 🥹
yay!
Transvaal lionOP
Thank you, sigh i realized what was the issue
{
"shared_list_id": 313113,
"resource_id": 2122
} is correct
{
'shared_list_id': 313113,
'resource_id': 2122
} i was using this
single quotes
ahh i forgot about checking json syntax
Transvaal lionOP
thank you again bro