cloudflare / workerd

The JavaScript / Wasm runtime that powers Cloudflare Workers
https://blog.cloudflare.com/workerd-open-source-workers-runtime/
Apache License 2.0
6.29k stars 308 forks source link

🐛 Bug Report — URLSearchParams body sets incorrect fetch content-type #3066

Open nickbabcock opened 2 weeks ago

nickbabcock commented 2 weeks ago
export default {
    async fetch(request, env, ctx) {
        const r = new Request('https://example.com/', {
            method: 'POST',
            body: new URLSearchParams({ hello: 'world' })
        });
        return new Response(r.headers.get('content-type'))
    },
};

Expected (ref):

application/x-www-form-urlencoded;charset=UTF-8

Actual:

text/plain;charset=UTF-8

This behavior differs from browsers and node.js

Replicated in cloudflare workers playground and in a production workers deployment.

balusch commented 2 days ago

It seems that api::url::URLSearchParams instead of api::URLSearchParams should be used in Body::Initializer:

https://github.com/cloudflare/workerd/blob/f32706b15ea3daa0e13a097d25d259d6ccf942be/src/workerd/api/http.h#L255