fastify example doesn't allow POST to pages/api handler
See original GitHub issueBug report
Describe the bug
Performing a POST to an API route when using a fastify server wrapper results in 400 Invalid body response.
To Reproduce
- Use a fastify example found in examples/custom-server-fastify
- Add the following in
/pages/api/test.js:
module.exports = (req,res) => {
const { body } = req;
res.json({ping: body})
}
Make a client request:
curl -v -POST -H"Content-Type: application/json" -d'{"ping":true}' http://localhost:3000/api/test
* Trying ::1...
* TCP_NODELAY set
* Connection failed
* connect to ::1 port 3000 failed: Connection refused
* Trying 127.0.0.1...
* TCP_NODELAY set
* Connected to localhost (127.0.0.1) port 3000 (#0)
> POST /api/test HTTP/1.1
> Host: localhost:3000
> User-Agent: curl/7.54.0
> Accept: */*
> Content-Type: application/json
> Content-Length: 13
>
* upload completely sent off: 13 out of 13 bytes
< HTTP/1.1 400 Invalid body
< Date: Tue, 07 Jan 2020 14:24:27 GMT
< Connection: keep-alive
< Transfer-Encoding: chunked
<
* Connection #0 to host localhost left intact
Invalid body%
Expected behavior
My API route handles the request.
System information
- Version of Next.js: 9.1.6
- version of fastify: 2.11.0
Additional context
It’s clear the error is coming from here: https://github.com/zeit/next.js/blob/1fd57d22d5f435e63caf7d0fbe1bcd99be49ea52/packages/next/next-server/server/api-utils.ts#L81-L86
I suspect there’s a double-body parse happening; e.g. fastify is already parsing the body then next is trying to do the same a second time. I tried to disable json parsing in fastify by doing:
fastify.addContentTypeParser('*', function (req, done) {
done()
})
as described here however it did not seem to have any effect. Still trying to work out a solution. I like the automatic route generation for modules under /pages/api so I would like to keep using that if possible. Otherwise I may have to move the API as plain fastify routes.
At a minimum the example README might want to add a caveat that API routes with content-body parsing will not work due to this interaction.
If I find a suitable workaround I’ll post an update here.
Issue Analytics
- State:
- Created 4 years ago
- Comments:5 (2 by maintainers)

Top Related StackOverflow Question
@Timer - can you elaborate on what you mean by the “now uses all” part of your comment?
We’re in the process of migrating our custom Next.js server from the vanilla Node.js server to Fastify and ran into this same issue. It appears that Fastify’s content parser has already consumed the request body by the time that Next.js tries to parse it in
api-utils.jsbefore passing the req to the API request handler.We were able to work around the issue by adding a no-op content parser in Fastify that leaves the body intact for these requests, but I’m curious if there’s a better way.
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.