Skip to content

Request body is omitted from POST requests in Next.js #2111

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

Open
1 task done
corydeppen opened this issue Jan 23, 2025 · 6 comments
Open
1 task done

Request body is omitted from POST requests in Next.js #2111

corydeppen opened this issue Jan 23, 2025 · 6 comments
Labels
bug Something isn't working openapi-fetch Relevant to the openapi-fetch library

Comments

@corydeppen
Copy link

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

@corydeppen corydeppen added bug Something isn't working openapi-fetch Relevant to the openapi-fetch library labels Jan 23, 2025
@kerwanp
Copy link
Contributor

kerwanp commented Jan 23, 2025

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?

@corydeppen
Copy link
Author

@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.

@corydeppen
Copy link
Author

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.

@vuongtrunghieu
Copy link

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.

@xLucaH
Copy link

xLucaH commented Mar 20, 2025

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?

@splicebison
Copy link

I just requested an update to the docs however perhaps this helps with your issues as well: #2242

My proposal and simple setup still allows for auth middleware as well.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
bug Something isn't working openapi-fetch Relevant to the openapi-fetch library
Projects
None yet
Development

No branches or pull requests

5 participants