openapi-typescript
openapi-typescript copied to clipboard
Request body is omitted from POST requests in Next.js
openapi-fetch version
0.13.4
Description
I'm trying to use openapi-fetch in a Next.js 14 app and I've noticed the body is being omitted from POST requests made from within server functions and RSCs. Specifically, prior to calling new CustomRequest(), requestInit.body contains the expected serialized/stringified object, however it's missing from the request instance that's created.
Admittedly, I haven't tried this in a different framework yet, so I'm not entirely sure whether this could be related to Next.js at this point. I just wanted to include that context in case it helps.
Reproduction
Make a POST request from a server function or RSC and observe the API request will be made without a body.
Expected result
Fetch requests containing a body that are made via openapi-fetch should pass the serialized data to the API as a standard fetch request would.
Extra
- [x] I’m willing to open a PR (see CONTRIBUTING.md)
Hi @corydeppen,
I haven't tried using Next 14 but I haven't encountered this issue with Next 15. Could you provide a minimal reproducible example?
@kerwanp After digging some more, my issue seems to be related to how fetch is called in my app. Before using openapi-fetch, I had been passing body as a part of the options object that's given to fetch (e.g. fetch(url, options)). I noticed that openapi-fetch is creating a Request and passing that to fetch instead (e.g. fetch(Request)). I'm wondering if the API (Azure Logic Apps) that's being called by my app isn't able to read the body/stream when the Request instance is used by fetch.
I recognize it doesn't exist today, but would it be feasible in the future to pass the options directly to fetch as the second arg instead of creating a request and passing that to fetch in cases where a custom Request isn't specified when the client is created? I don't know how common it is for APIs to be unable to read the body when it's a ReadableStream.
Hi @corydeppen,
I haven't tried using Next 14 but I haven't encountered this issue with Next 15. Could you provide a minimal reproducible example?
I also face the issue with Server function/action in Next.js 14, but not in Next.js 15. Will try to make a reproducible repo.
I’m facing this issue in Next.js 15 as well. Interestingly, it only occurs in production builds (next build && next start) and not during development.
For POST requests, I had to pass a custom fetch function that explicitly sets "Content-Type": "application/json" and includes my Authorization headers for protected routes.
Additionally, I ran into issues with the next cache options in fetch for GET requests—these options were being omitted. I was able to work around this by explicitly passing fetch to the client call:
const { data } = await client.GET('/api/products', {
fetch
});
This allowed the fetch + caching logic to work as expected.
While this workaround works for now, it's not ideal. It would be great to understand whether this is a limitation of openapi-fetch or if there’s a way to handle this more cleanly in production. Has anyone found a better solution?
I just requested an update to the docs however perhaps this helps with your issues as well: https://github.com/openapi-ts/openapi-typescript/issues/2242
My proposal and simple setup still allows for auth middleware as well.