question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

next.359 - load fetch POST => Request with GET/HEAD method cannot have body

See original GitHub issue

Describe the bug

In a load function of a page, doing a fetch to http://localhost:3000/api will throw an error:

Request with GET/HEAD method cannot have body.

Here is the code:

    const resultPOST = await event.fetch("http://localhost:3000/api", {
      method: "POST",
      headers: {
        "Content-Type": "application/json",
      },
      body: JSON.stringify({
        hello: "World",
      }),
    });
    const jsonPOST = await resultPOST.json();

Info

I stripped down the reproduction to http://localhost:3000/api URL. But my use case will be to target other APIs not only locals one like ‘/api’

Reproduction

Here is the stackblitz: https://stackblitz.com/fork/github/jycouet/sk-359-post (fetch issue is happening, but the log is different than locally!)

Here is the minimal repo: https://github.com/jycouet/sk-359-post With the few step in git from npx create to the issue.

Logs

Request with GET/HEAD method cannot have body.
TypeError: Request with GET/HEAD method cannot have body.
    at new Request (file:///home/jycouet/udev/tmp/sk-359-post/node_modules/@sveltejs/kit/dist/node/polyfills.js:9104:14)
    at Agent$1.fetch (file:///home/jycouet/udev/tmp/sk-359-post/node_modules/@sveltejs/kit/dist/node/polyfills.js:10280:22)
    at fetch (file:///home/jycouet/udev/tmp/sk-359-post/node_modules/@sveltejs/kit/dist/node/polyfills.js:12200:22)
    at Object.fetch (file:///home/jycouet/udev/tmp/sk-359-post/.svelte-kit/runtime/server/index.js:2293:51)
    at load (index.svelte:17:35)
    at runMicrotasks (<anonymous>)
    at processTicksAndRejections (node:internal/process/task_queues:96:5)
    at async load_node (file:///home/jycouet/udev/tmp/sk-359-post/.svelte-kit/runtime/server/index.js:2394:12)
    at async respond$1 (file:///home/jycouet/udev/tmp/sk-359-post/.svelte-kit/runtime/server/index.js:2778:15)
    at async resolve (file:///home/jycouet/udev/tmp/sk-359-post/.svelte-kit/runtime/server/index.js:3291:11)

System Info

System:
    OS: Linux 5.4 Ubuntu 20.04.2 LTS (Focal Fossa)
    CPU: (8) x64 11th Gen Intel(R) Core(TM) i7-1165G7 @ 2.80GHz
    Memory: 1.24 GB / 7.69 GB
    Container: Yes
    Shell: 5.0.17 - /bin/bash
  Binaries:
    Node: 16.15.1 - ~/.volta/tools/image/node/16.15.1/bin/node
    Yarn: 1.22.17 - ~/.volta/tools/image/yarn/1.22.17/bin/yarn
    npm: 8.3.0 - ~/.volta/tools/image/npm/8.3.0/bin/npm
  npmPackages:
    @sveltejs/adapter-auto: next => 1.0.0-next.53 
    @sveltejs/kit: 1.0.0-next.359 => 1.0.0-next.359 
    svelte: ^3.44.0 => 3.48.0 
    vite: 2.9.13 => 2.9.13

Severity

blocking an upgrade

Additional Information

It’s a blocker for our Library Houdini that was working well before next.359. We have an issue open here: https://github.com/HoudiniGraphql/houdini/issues/374

Issue Analytics

  • State:closed
  • Created a year ago
  • Reactions:6
  • Comments:9 (8 by maintainers)

github_iconTop GitHub Comments

2reactions
GauBencommented, Jul 8, 2022
1reaction
GauBencommented, Jul 6, 2022

You can also try patching node_modules/@sveltejs/kit/assets/server/index.js:2304 with:

const external_request = new Request(requested, /** @type {RequestInit} */ (opts));
response = await options.hooks.externalFetch.call(null, external_request.url, external_request);

and restart the dev server.

Read more comments on GitHub >

github_iconTop Results From Across the Web

My POST request is failing with a 'Request with GET/HEAD ...
I've got my answer in the comments but just posting an answer just in case someone else is facing 'REQUEST WITH GET/HEAD METHOD'...
Read more >
Request with GET/HEAD method cannot have body. #551
So, anyone using a body on a GET is effectively "off the reservation"; they're not longer using HTTP, but defining their own private...
Read more >
[Fetch] Request with GET/HEAD method cannot have body ...
Message: Seems spec was updated after the code. Description: [Fetch] Request with GET/HEAD method cannot have body. BUG= ...
Read more >
Failed to execute 'fetch' on 'Window': Request with GET cannot ...
Resolutions steps and guidelines for error "Failed to execute 'fetch' on 'Window': Request with GET/HEAD method cannot have the body".
Read more >
Using the Fetch API - MDN Web Docs
The Fetch API provides a JavaScript interface for accessing and manipulating parts of the protocol, such as requests and responses.
Read more >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found