http request stalling because of a non empty body??
Unanswered
Forest yellowjacket posted this in #help-forum
Forest yellowjacketOP
i dont actually know how to explain what is happening
this is supposed to prevent any other methods from working other than
and it sort of works
if i try a GET request with an empty body: it does return a 404 (intended solution)
if i try a POST request with a body: it does work and returns a 200 (intended solution)
if i try to do a GET request with a body: it does not work???
it stalls
it doesnt log / prints anything to console
it doesnt even finish the request
STEPS TO REPRODUCE:
1. create a new nextjs Project with src/ eslint
2. i dont think this has anything to do with the problem but ill just include it
init a prisma db with vercel postgres
3. i also have the class-validator package just so i can validate the body (it doesnt work even when the validate function is commented out)
4. make a new api endpoint /api/test.ts
5. code:
6.
7. make a GET request with some body data
Please help im super annoyed at this problem ive been trying to fix it for the last 3 hours
if (!(req.method === 'POST')) { res.status(404).end(); return }this is supposed to prevent any other methods from working other than
POSTand it sort of works
if i try a GET request with an empty body: it does return a 404 (intended solution)
if i try a POST request with a body: it does work and returns a 200 (intended solution)
if i try to do a GET request with a body: it does not work???
it stalls
it doesnt log / prints anything to console
it doesnt even finish the request
STEPS TO REPRODUCE:
1. create a new nextjs Project with src/ eslint
2. i dont think this has anything to do with the problem but ill just include it
init a prisma db with vercel postgres
3. i also have the class-validator package just so i can validate the body (it doesnt work even when the validate function is commented out)
4. make a new api endpoint /api/test.ts
5. code:
export default async function handler(
req: NextApiRequest,
res: NextApiResponse
) {
if (!(req.method === 'POST')) { res.status(404); return }
res.status(200).send("it works"); return
}6.
yarn run dev7. make a GET request with some body data
Please help im super annoyed at this problem ive been trying to fix it for the last 3 hours
49 Replies
Get requests can't have any body?
Forest yellowjacketOP
yes but in other projects it ussualy doesnt care about the body
Forest yellowjacketOP
i have another nextjs project configured the same way with updated stuff
but it doesnt blame or stall it just sends the 404
i dont know what happened i just dont understand please help
but it doesnt blame or stall it just sends the 404
i dont know what happened i just dont understand please help
@@ts-ignore Get requests can't have any body?
Forest yellowjacketOP
i didnt actually know this thanks btw
but if a get requests cant have body than what happened with my other project
but if a get requests cant have body than what happened with my other project
let me try to reproduce
Forest yellowjacketOP
also i assume if a get request cant have a body the request would cancel or error out
but it just doesnt do anything
but it just doesnt do anything
@Forest yellowjacket also i assume if a get request cant have a body the request would cancel or error out
but it just doesnt do anything
this worked
export default async function handler(req, res) {
if (!req.method === "POST") {
res.status(404);
return
}
res.status(200).send("it works");
return;
}note that extra pair of brackets in
if statementI removed them and it worked
Forest yellowjacketOP
well isnt
im pretty sure that means not req.method
like if
and
if (!req.method === "POST")im pretty sure that means not req.method
like if
req.method does exist then reverse it to Falseand
False is not equal to POST so it deffinetly would send the 200or am i mistaken??
Forest yellowjacketOP
well its supposed to throw a 404
yes it should
Forest yellowjacketOP
what is happening
also i just tested it even more
Now it stalls even without a body
Now it stalls even without a body
@Forest yellowjacket also i just tested it even more
Now it stalls even without a body
export default async function handler(req, res) {
if (!(req.method === "POST")) {
return res.status(404).end();
}
return res.status(200).send("it works");
} hereForest yellowjacketOP
yes that works
ig it just doesnt work with a body in a get request
API handler should not return a value, received object.but know this is logged out
should i be worried about this?
Forest yellowjacketOP
hm
im on nextjsj^13.4.8
Forest yellowjacketOP
what
@Forest yellowjacket what
yeah no response, no warning
adding
return in new line will fix that tooForest yellowjacketOP
not even the api handler should not even return object thing?
can i send u a liveshare to show u what i mean? bcs its kinda hard to explain in discord
@Forest yellowjacket can i send u a liveshare to show u what i mean? bcs its kinda hard to explain in discord
the warning you're getting is because we're doing
return res.status(...) removing return from there and adding it to next line fixed thatAmerican Crocodile
Now i am seeing the same problem
@@ts-ignore the warning you're getting is because we're doing `return res.status(...)` removing return from there and adding it to next line fixed that
Forest yellowjacketOP
is that intended like is it supposed to be next line return?
@American Crocodile Now i am seeing the same problem
Forest yellowjacketOP
like the get request or the no object return?
bcs both are very annoying
@@ts-ignore ig so
Forest yellowjacketOP
huh
@Forest yellowjacket like the get request or the no object return?
American Crocodile
API handler should not return a value, received object.
Forest yellowjacketOP
back to the point,
what about the Get Request stalling thing? is that also intended feature?
what about the Get Request stalling thing? is that also intended feature?
Forest yellowjacketOP
So should I mark this as done
BCS it's not fixed
And I am confused
BCS the docs didnt exactly say I can't add body data to a get request it just says I shouldnt
Well you already got your answer. Nextjs api routes can’t take body for GET (and similarly, DELETE)
Forest yellowjacketOP
So therefor the docs is wrong?