I’m writing a worker that takes in client API requests, inspects them to see if the response is cacheable and forwards the original request to the origin if it is not.
I need to gather up arguments from all the different ways our API endpoints allow the client to pass parameters (GET, POST, in the top level of the payload, etc.). When I call “await request.formData()” to get the contents of the POST body, I get the arguments passed by the client, canonize the request format and create a new request to our edge-cacheable URL location.
The problem comes when my worker logic determines that the API call should actually not be cached and instead the original request should simply be forwarded to the origin ( “return fetch( request );” ). When I try to forward the original request I get this fatal error:
“Uncaught (in promise) TypeError: This readable stream is currently locked to a reader.”
I tracked it down to the “await request.formData()” call. I tried a lot of stuff (playing around with request.body.getReader() etc.) but haven’t been able to figure anything out.
I suppose I could always manually reconstruct a new request that looks exactly like the one passed in to my worker, but I would expect to be able to just forward the original request like I have been before I needed to get the POST parameters. If feel like there is probably just something I am doing wrong or missing.