Skip to content

+server.ts API endpoint fails to parse body #7409

Description

@callmeberzerker

Describe the bug

The only file new file in the blank svelte-kit project is the +server.ts located in src/routes/api/+server.ts. It just echoes the incoming JSON. Hitting http://localhost:5173/api (with content-type and accept headers set to application/json and body { "sample": "test"} ) will return HTTP 400 Bad request -> and the request never hits the API endpoint.

If I remove the content-type and accept headers it will enter the API endpoint but it would error out on await request.json() with:

Unexpected end of JSON input
SyntaxError: Unexpected end of JSON input

It might be a Windows issue but honestly I am not sure and can't test atm.

Reproduction

https://github.com/callmeberzerker/body-not-working

Run pnpm install and then pnpm dev.

Logs

Sadly there are none.

System Info

System:
    OS: Windows 10 10.0.22621
    CPU: (16) x64 Intel(R) Core(TM) i9-9900K CPU @ 3.60GHz
    Memory: 16.30 GB / 31.92 GB
  Binaries:
    Node: 19.0.0 - C:\Program Files\nodejs\node.EXE
    Yarn: 1.22.19 - C:\Program Files\nodejs\yarn.CMD
    npm: 8.19.2 - C:\Program Files\nodejs\npm.CMD
  Browsers:
    Edge: Spartan (44.22621.674.0), Chromium (105.0.1343.42)
    Internet Explorer: 11.0.22621.1
  npmPackages:
    vite: ^3.2.0 => 3.2.0

Severity

blocking all usage of SvelteKit

Additional Information

N/A

Activity

  1. georgdonner commented on Oct 27, 2022

    @georgdonner

    I ran into the exact same issue today setting up a new Sveltekit project and am on MacOS, so it's probably not a Windows issue. It might be related to #5370, because I am seeing the same error "Invalid request body"

  2. Rich-Harris commented on Oct 27, 2022

    @Rich-Harris
    Member

    Oddly, I can reproduce this by cloning the repo, but as soon as I add a pnpm override to start debugging it, it works... a true Heisenbug 🤔

  3. callmeberzerker commented on Oct 27, 2022

    @callmeberzerker
    Author

    Thanks for the response - I almost went crazy trying out different things for this seemingly straight-forward thing!

  4. Conduitry commented on Oct 27, 2022

    @Conduitry
    Member

    I think this is due to the new version of undici that was just released a few hours ago. Its Request implementation is throwing RequestInit: duplex option is required when sending a body. for the incoming request.

  5. changed the title [-]+server.ts API endpoint fails to parse body (possible Windows issue?)[/-] [+]+server.ts API endpoint fails to parse body[/+] on Oct 27, 2022
  6. Conduitry commented on Oct 27, 2022

    @Conduitry
    Member

    Yeah, when I override to 5.11.0, things seem to work fine. The obvious change in 5.12.0 that would be causing this is nodejs/undici#1681 but I haven't dug any further into what this was intended to do or whether this is a new bug there or what.

  7. added
    bugSomething isn't working
    p0-urgentSvelteKit is broken or vulnerable for most users
    on Oct 27, 2022
  8. Conduitry commented on Oct 27, 2022

    @Conduitry
    Member

    I'm marking this as urgent. It might be a good idea to pin to undici 5.11.0 if we can't immediately see how we should now be calling Request now or if it looks like the new behavior is a bug.

  9. callmeberzerker commented on Oct 27, 2022

    @callmeberzerker
    Author

    Yep, I can confirm pinning undici@5.11.0 via pnpm.overrides (https://pnpm.io/package_json#pnpmoverrides) fixed the issue on my project for the time being. Thanks! 🙌

  10. KhafraDev commented on Oct 27, 2022

    @KhafraDev
  11. careerlister commented on Oct 27, 2022

    @careerlister
    Contributor

    Is there a work around or will the patch be coming soon?

  12. Conduitry commented on Oct 27, 2022

    @Conduitry
    Member

    Until #7412 is released, the workaround is to use overrides to force undici@5.11.0.

  13. added theissue type on Aug 4, 2026
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    p0-urgentSvelteKit is broken or vulnerable for most users

    Type

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions