Skip to content
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

Getting error while trying to use KY to fetch data in NEXTJS 13 Server APIs 'TypeError: Cannot construct a Request with a Request object that has already been used.' #486

Open
FaizanShaikh45 opened this issue Jan 25, 2023 · 11 comments

Comments

@FaizanShaikh45
Copy link

FaizanShaikh45 commented Jan 25, 2023

Hi Guys,

Config:

KY Version: ^0.33.2
Node Version: 18.13.0
NextJS Version: latest (13)
React: 18.2.0

I'm trying to use KY to retrieve data from backend services. I'm using KY in the NextJS APIs folder to call down-stream services. However, I am receiving the error mentioned below.

TypeError: Cannot construct a Request with a Request object that has already been used.
    at Object.fetch (node:internal/deps/undici/undici:14062:11)
    at async fn (webpack-internal:///(api)/./node_modules/ky/distribution/core/Ky.js:29:28)
    at async result.<computed> [as json] (webpack-internal:///(api)/./node_modules/ky/distribution/core/Ky.js:65:39)
    at async handler (webpack-internal:///(api)/./pages/api/user/recommended-user-list.ts:13:30)
    at async Object.apiResolver (folder/node_modules/next/dist/server/api-utils/node.js:372:9)
    at async DevServer.runApi (folder/node_modules/next/dist/server/next-server.js:488:9)
    at async Object.fn (folder/node_modules/next/dist/server/next-server.js:751:37)
    at async Router.execute (folder/node_modules/next/dist/server/router.js:253:36)
    at async DevServer.run (folder/node_modules/next/dist/server/base-server.js:384:29)
    at async DevServer.run (folder/node_modules/next/dist/server/dev/next-dev-server.js:741:20)
    at async DevServer.handleRequest (folder/node_modules/next/dist/server/base-server.js:322:20)

HTTP Client Code Ref: image

The structure I'm attempting to set up for the http client is seen above. So I'm trying to create an HTTP client and passing it to the service as arguments to the function, and then with that client service is calling downstream APIs to get data.

Service Code Ref:
image

It works perfectly with NextJS 12, however when I try to implement it with NextJS 13, I get this problem. Could someone please assist me with this?

Thanks

@panudetjt
Copy link

Have you figured it out ?

My assumption is because nextjs is polyfill the native fetch with their own version of fetch.

@Talent30
Copy link

The fetch in NextJS behaves differently to the native fetch.

@Talent30
Copy link

I can reproduce this, maybe need to raise an issue on next.js side

@AlexisWalravens
Copy link

I also encountered this issue, I tried with just the native NodeJS fetch (polyfilled by Next) and I don't have the issue with just fetch.

@vladly
Copy link

vladly commented Apr 21, 2023

I also faced this problem.

@lcdss
Copy link

lcdss commented Jun 26, 2023

Same problem here. I'll probably use just fetch for now.

@AlexisWalravens
Copy link

Same problem here. I'll probably use just fetch for now.

I now use ofetch (https://github.com/unjs/ofetch), it has a similar set of features as ky and it works with the app router.

@mabdullahadeel
Copy link

I am also having troubles getting ky to work in nextjs server actions/components

In my case, it does make request but the json payload is not attached to the request. Using fetch solved the issue. So I guess it's something related to ky polyfill.

@sholladay
Copy link
Collaborator

sholladay commented Aug 16, 2023

Please try awaiting the response before calling .json() and tell me if that fixes it or not.

Before:

return httpClient
    .post ("test/test", {
        json: {
            test: params. test,
        }
    })
    .json()

After:

const response = await httpClient
    .post ("test/test", {
        json: {
            test: params. test,
        }
    });

return response.json();

@GLObus303
Copy link

I ran into similar issues with the Next.js app folder/server actions/server components or whatnot. The solution was quite simple for me and it was upgrading to Node 18+.

Apparently, Node pre-18 is not supporting native fetch and I do suppose Next.js has troubles with that. Unfortunately, the fact Ky is node 18+ is at the bottom of the docs, so it took a while to find out.

@IRediTOTO
Copy link

lso having troubles getting ky to work in nextjs server actions/components

In my case, it does make request but the json payload is not attached to the request. Using fetch solved the issue. So I guess it's something related to ky polyfill.

I just tried, not working

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

10 participants