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.

Endpoint Response breaks with "content-type: application/json"

See original GitHub issue

Describe the bug

Using the new api described in #3384, when I create an API endpoint returning a Response, it breaks in Cloudflare Workers if I set content-type: application/json as a Response header.

I believe the reason is because it applies JSON.stringify to the body of the response. However the body of the response being a ReadableStream, it breaks returning an object { locked: false }.

Reproduction

I believe the culprit is found here: https://github.com/sveltejs/kit/blob/ee906a3b2b103e4523eb1b6bb13f04d03eb35238/packages/kit/src/runtime/server/endpoint.js#L86

Normally the control is_pojo(body) shouldn’t allow the code to reach here, however the control doesn’t work correctly in Cloudflare Workers. More specifically it fails here: https://github.com/sveltejs/kit/blob/ee906a3b2b103e4523eb1b6bb13f04d03eb35238/packages/kit/src/runtime/server/endpoint.js#L119

In Cloudflare body[Symbol.toStringTag] returns undefined. Replacing the control with body instanceof ReadableStream works in Cloudflare Workers.

Logs

No response

System Info

Binaries:
    Node: 16.10.0 - ~/.volta/tools/image/node/16.10.0/bin/node
    Yarn: 3.1.0 - ~/.volta/tools/image/yarn/1.22.17/bin/yarn
    npm: 8.3.1 - ~/.volta/tools/image/npm/8.3.1/bin/npm
  npmPackages:
    @sveltejs/adapter-cloudflare: ^1.0.0-next.8 => 1.0.0-next.8 
    @sveltejs/kit: ^1.0.0-next.239 => 1.0.0-next.239 
    svelte: ^3.46.2 => 3.46.2 
    vite: ^2.7.12 => 2.7.12

Severity

serious, but I can work around it

Additional Information

No response

Issue Analytics

  • State:closed
  • Created 2 years ago
  • Comments:8 (8 by maintainers)

github_iconTop GitHub Comments

1reaction
vicky1999commented, Jan 29, 2022

okay. Thanks. I tried to run the test cases. all the test cases were passing

0reactions
bluwycommented, Jan 29, 2022

@vicky1999 Not sure how easy it is to reproduce this, since it happens on cloudflare specifically, but as long as the test pass with your changes. I think that’s good enough.

Read more comments on GitHub >

github_iconTop Results From Across the Web

Running an API, if I correct a content-type header will that ...
I can imagine a library where the content-type: application/json automatically causes a json parser to get involved but I don't know of any ......
Read more >
Asp.net web api sometimes returning content type as text/plain
I understand response depends on the content-type header. I am asking this question assuming client will always send content-type as application ...
Read more >
Why do I get an invalid content type when calling
The error I receive is Invalid Content Type. I've added 'application/json' the the content-type header but still I have no success.
Read more >
REST API - Open Policy Agent
Content -Type: application/x-yaml: Indicates the request body is a YAML encoded object. Query Parameters. pretty - If parameter is true , response will...
Read more >
Minimal APIs quick reference - Microsoft Learn
The following command makes the app respond to port 7777 : ... If the Kestrel endpoint is also configured in the appsettings.json file, ......
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