Parse URL query strings with Cloudflare Workers

I’m using the following method

async function handleRequest(request) {
  const params = {}
  const url = new URL(request.url)
  const queryString = url.search.slice(1).split('&')

  queryString.forEach(item => {
    const kv = item.split('=')
    if (kv[0]) params[kv[0]] = kv[1] || true
  })

  console.log(JSON.stringify(params))
  return new Response(JSON.stringify(params))

It allows looking up query string parameters via params.name
Anything wrong with that?

1 Like