Cannot convert FormData to URLSearchParams in workers script

I try to convert FormData to URLSearchParams inside workers but it returns empty string.

Here is the example code:

addEventListener('fetch', event => {
  event.respondWith(handleRequest(event.request))
})

/**
 * Fetch and log a given request object
 * @param {Request} request
 */
async function handleRequest(request) {
  const formData = new FormData();
  formData.set("key", "value");
  const url = new URLSearchParams(formData);
  return new Response(url.toString());
}

Link to the playground

Based on the documentation:

You can also pass it directly to the URLSearchParams constructor if you want to generate query parameters in the way a <form> would do if it were using simple GET submission.

In Chrome, the result is expected key=value, but in Cloudflare Workers it returns empty string.

Is this expected in Cloudflare workers environment?

Seems to be reproducible

@KentonVarda

Yep, this looks like a bug in our implementation. I’ve raised an internal ticket (#3421) about this. I don’t know when we’ll have a chance to fix it. We’ll probably have to do an analysis to make sure that no one is currently inadvertently depending on the buggy behavior. In the meantime, you’ll have to work around the problem by manually iterating over the FormData and copying each key/value over to URLSearchParams.

@sandro @KentonVarda Ok, thanks for the confirmation :+1:

URLSearchParams also turns up empty .keys() and .entries():

const isit = new URL(“https://example.com/?where=isit").searchParams.get("where”); //“isit”
const NOwhere = new URL(“https://example.com/?where=isit”).searchParams.keys(); //{}