I wrote a cloudflare worker to check headers for authorization. If it is missing, just forward the requests. I found most requests are ok, but some post requests throws errors like this:
"exceptions": [
{
"name": "TypeError",
"message": "Cannot reconstruct a Request with a used body.",
"timestamp": 1688611154771
}
],
"logs": [
{
"message": [
"forwarded: https://..."
],
"level": "log",
"timestamp": 1688611154771
}
]
And the code throws exception is like this:
export default {
async fetch(request, env, ctx) {
// let error pass to the origin server
ctx.passThroughOnException();
let fwd_request = new Request(request);
const headers = request.headers;
const auth = headers.get('Authorization')
if (!auth) {
console.log(`forwarded: ${request.url}`)
return fetch(request);
}
...
So, the console.log is printed out, and fetch(request) doesn't change the original request.
Why I am getting this error?