New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Next.js 9 API route works on Get Request but not Post request #7960
Next.js 9 API route works on Get Request but not Post request #7960
Comments
Hey @zluo01, provide full reproduction, from your issue it's hard to guess what could be wrong. You can check integration test for |
Were you able to figure this out? The documentation for API routes is pretty minimal, and I'm also receiving a 404 response for POST requests sent to a route that worked fine for GET requests |
Sorry no, I use Get and put stuff inside headers instead and I do not have time to produce a full reproduction. |
Some documentation on handling POST would be useful |
@stoplion what would you like to have documented? Can you create an issue following the issue template for it? |
Sure |
I also see the 404 in our project with the following: const res = await fetch('/api/bookings', {
method: 'POST',
body: JSON.stringify({ hungry: true }),
headers: {
'Content-Type': 'application/json',
},
})
const data = await res.json() It dosent matter what the content is in the Update server.post('*', (req, res) => {
return handle(req, res)
}) |
@TommySorensen this should be fixed in the latest canary |
Was this ever answered? Is there a way to POST to a Next v9 API Route? |
Yes. Just posting to the api routes will work just fine. Use this example and change the get to post. Thats what i did. Worked like a charm 😉 |
@huv1k any idea on a release? |
@lifeiscontent release of what? |
@huv1k on when the next release of next.js will come out with this fix |
@lifeiscontent There is no bug to fix. I tested it my self. POST work just fine if you use of the NextJS examples. It's properly something in your application. Please provide example of the full code, so we can help with the investigation 👍 |
easy deasy server.use((req, res) => app.getRequestHandler()(req, res)) |
Repro:
Add file api/test.js
Add request handler in server.js:
Run Send POST to http://localhost:3000/api/test Expected: console logs |
You need to update the example:
needs to be:
|
Found our mistake: upgrading By the way, Firebase dependency is quite old:
|
If anyone has this issue yet with a custom express server, it can occur while using multiple body parsers, because NextJs api routes enables it by default. Use only express body parser, or only NextJs body parser. How to disable NextJs body parser documented there: https://nextjs.org/docs#api-middlewares |
@epranka @thgh, Man, it took me hours to figure this one out. I'm using the Firebase Cloud Functions server, and I couldn't make any POST requests to my API route. POST requests would hang, but GET requests would work fine. And if I was using the local NextJS dev server, POST requests would also work fine. Disabling the body parser when using Firebase Cloud Functions fixed it. Apparently Firebase Cloud Functions comes with its own body parser, and it conflicts with the NextJS one. So you have to disable the body parser for Firebase Cloud Functions, but enable it for local development using the NextJS dev server. So basically drop this in your export const config = {
api: {
bodyParser: process.env.NODE_ENV !== 'production'
}
}; |
@TommySorensen answer solved it for me:
Many thanks! |
Thanks! Was facing the exact same issue. Resolved with your fix. Its surprising that Firebase functions do not give any config to turn off automatic body parsing. |
so we have to use custom server, got it but getting the parsed body inside api route would have solved many problems. |
…16169) ## Why Some users prefer to use a custom server implementation that handles body parsing. If they do this, they have no way to opt out of all body parsing in API Routes. Requests with bodies die if next's `bodyParser` is not disabled. Requests just hang forever. Instead of adding [this config](https://nextjs.org/docs/api-routes/api-middlewares#custom-config) to every API Route, we do a simple check to avoid parsing the body twice. Fixes #8315 Fixes #7960
We had in our ExpressJs Changed it to be route specific: https://github.com/expressjs/body-parser#express-route-specific and then it works :) |
Hmm, I wonder why no one gave attention to that (get -> get, post -> post) 😆 , thanks, you saved my day. |
This issue has been automatically locked due to no recent activity. If you are running into a similar issue, please create a new issue with the steps to reproduce. Thank you. |
Question about Next.js
I try to modified some of the api to use post instead of get.
I have request code like this with normal fetch
It will get to the file inside ./page/api and resolve, however, if I change to code to following
It will give me 404 not found. I wonder what is the problem here ?
Thank you
The text was updated successfully, but these errors were encountered: