Request content-type header set incorrectly

There’s a bug in the fetch/Request implementation. When the request body is an instance of URLSearchParams the request’s content-type header should automatically be set to “application/x-www-form-urlencoded”

https://fetch.spec.whatwg.org/#request-create

To reproduce, try this code in a browser vs in a worker.

const r = new Request('https://example.com/', {
  method: 'POST',
  body: new URLSearchParams({ hello: 'world' })
});
console.log(r.headers.get('content-type'));
// expected: application/x-www-form-urlencoded;charset=UTF-8
// actual:   text/plain;charset=UTF-8

@harris I think I might have found another bug with fetch similar to this one Multipart/form-data field names are encoded incorrectly - #5 by jdanyow

Hi @jdanyow, thank you for the report.

Could you post a minimal, complete reproduction? When I tried the code snippet you posted in our fiddle, it works as expected: Cloudflare Workers. I also manually inspected our underlying implementation, and it looks correct to me.

Could it be that you’re using a URLSearchParams polyfill, and maybe we’re not recognizing it as URLSearchParams?

@harris I think the team found the issue- here’s the PR. Can close this thread out.