Bug: URLSearchParams as a Fetch API body uses the wrong Content-Type
See original GitHub issue[Bug]
What
The Fetch API
determines the wrong Content-Type
header when its body is set to a polyfilled URLSearchParams
instance.
Details
This occurs when the following statements are true:
- The
URLSearchParams
object has been polyfilled. - A POST request is made using the Fetch API.
- The POST request’s body is set to an instance of the polyfilled
URLSearchParams
object. - The POST request’s
Content-Type
header has NOT been set manually.
Safari on iOS seems to use the polyfilled URLSearchParams
, so that’s what I’ve used to debug the issue.
I’ve created a sample project (https://github.com/tobyaherbert/urlsearchparams-bug) and published it (https://urlsearchparams-bug.nialto.workers.dev) so you can see the problem.
Here are some screenshots of it working as expected (no bugs) in Google Chrome on macOS:
Also working in Safari on macOS:
But not working in MobileSafari on iOS:
Issue Analytics
- State:
- Created 2 years ago
- Comments:7
Top Results From Across the Web
Bug: URLSearchParams as a Fetch API body uses the wrong ...
The Fetch API determines the wrong Content-Type header when its body is set to a polyfilled URLSearchParams instance. Details. This occurs when the...
Read more >How do I post form data with fetch api? - Stack Overflow
You can set body to an instance of URLSearchParams with query string passed as ... Note that with axios I had to use...
Read more >Using the Fetch API with URLSearchParams in JavaScript
The new URLSearchParams Object integrates really well with the Fetch API. In this video I'll be going over how you can easily combine...
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 >4 common mistakes front-end developers make when using ...
4 common mistakes front-end developers make when using fetch · 1. Assuming that the promise rejects on HTTP error statuses · 2. Forgetting...
Read more >Top Related Medium Post
No results found
Top Related StackOverflow Question
No results found
Troubleshoot Live Code
Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start FreeTop Related Reddit Thread
No results found
Top Related Hackernoon Post
No results found
Top Related Tweet
No results found
Top Related Dev.to Post
No results found
Top Related Hashnode Post
No results found
Top GitHub Comments
@romainmenke Trying to create a minimal reproducible example, I found that the
fetch
polyfill ignored inferredContent-Type
when theheaders
option is given asobject
.So I opened another issue there. https://github.com/github/fetch/issues/1076
I think there are 3 separate issues here :
default
feature alias includesURL
but notfetch
. For this feature to work both are required. (@tobyaherbert)1 and 2 are related, but 1 should be resolved by including
fetch
in the polyfill url. Even if 2 is breaking stuff in recent Safari iOS versions, older browsers will also be broken without includingfetch
.@en30 I tried to verify if the
URL
orfetch
polyfill break this feature if the browser has native support. For me it all seems to work.My steps :
fetch
Content-Type
POST
request withURLSearchParams
to this endpointURL
andfetch
so that the polyfills are served to newer Safari versions (those with native support)(https://github.com/romainmenke/polyfill-library/pull/71/commits/4c37f51411081ee56bfe81766f595c17a15cbfee)
I tried both the polyfill bundle from the test suite and a hard coded url from the production service (
https://polyfill.io/v3/polyfill.min.js?version=3.108.0&features=URL,fetch
)I was unable to to get a failure when both
URL
andfetch
are polyfilled in Safari browsers (iOS and macOS)Results from CI suite : https://github.com/romainmenke/polyfill-library/runs/4059057445?check_suite_focus=true (IE fails here because I was lazy and kept
console.log
statements)Is it possible that you have other polyfills? For example
core-js
manually or viapreset-env
.At this point only the user agent issue is actionable I think, but this should be moved to a new issue.