Can workers convert POST requests to GET?

If I send a POST request with body to my worker and use the following code (as instructed here),

  if (request.method === 'POST') {
    const newRequestInit = {
      method: 'GET',
    }
    const newRequest = new Request(url.toString(), new Request(request, newRequestInit))
}

I get the error “TypeError: Request with GET/HEAD method cannot have body.”

Is there an easy way to strip the body from the incoming request and keep the rest of the request parameters?

My ultimate aim here is to avoid sending preflight requests to workers by using a “simple request”. I wanted to send some content via the body, but if I can’t do that I’ll just put it in the URL. Thanks!

I’ll answer my own question. I just looped through the headers and copied what I needed:

      const newRequest = new Request(url.toString(), newRequestInit)
      for (const [key, value] of request.headers.entries()) {
        if (! ['content-length', 'content-type'].includes(key.toLowerCase())) {
          newRequest.headers.set(key, value)
        }
      }
1 Like

This topic was automatically closed 3 days after the last reply. New replies are no longer allowed.