Cloudflare Worker 无法转发我的部分 POST 请求。

huangapple go评论43阅读模式
英文:

cloudflare worker can't forward some of my post requests

问题

我编写了一个Cloudflare Worker来检查授权头。如果缺少授权头,只需转发请求。我发现大多数请求都没问题,但一些POST请求会抛出以下错误:

"exceptions": [
    {
        "name": "TypeError",
        "message": "Cannot reconstruct a Request with a used body.",
        "timestamp": 1688611154771
    }
],
"logs": [
    {
        "message": [
            "forwarded: https://..."
        ],
        "level": "log",
        "timestamp": 1688611154771
    }
]

而引发异常的代码如下:

export default {
    async fetch(request, env, ctx) {
        // 让错误传递到原始服务器
        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);
        }
        ...
    }
}

所以,console.log被打印出来,但fetch(request)不会改变原始请求。

我为什么会收到这个错误?

英文:

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?

答案1

得分: 1

这一行:

let fwd_request = new Request(request);

消耗了原始的 request 的主体,并将其传递给 fwd_request。如果在此之后尝试执行 fetch(request),并且请求具有主体,它将无法工作。您可以改为执行 fetch(fwd_request),或者您可以延迟构建 fwd_request,直到 if (!auth) 块之后,因为在该块之前似乎不会使用它。

export default {
  async fetch(request, env, ctx) {
    // 让错误传递到原始服务器
    ctx.passThroughOnException();

    const headers = request.headers;
    const auth = headers.get('Authorization')
    if (!auth) {       
     console.log(`转发请求:${request.url}`)
     return fetch(request);
    }
    let fwd_request = new Request(request);
    ...
英文:

This line:

let fwd_request = new Request(request);

consumes the original request's body, and transfers it to fwd_request. If you try to do fetch(request) after this, and it has a body, it won't work. You could do fetch(fwd_request) instead, or you could delay constructing fwd_request until after the if (!auth) block, since it looks like you don't use it before that block anyway.

export default {
  async fetch(request, env, ctx) {
    // let error pass to the origin server
    ctx.passThroughOnException();

    const headers = request.headers;
    const auth = headers.get('Authorization')
    if (!auth) {       
     console.log(`forwarded: ${request.url}`)
     return fetch(request);
    }
    let fwd_request = new Request(request);
    ...

huangapple
  • 本文由 发表于 2023年7月18日 16:35:30
  • 转载请务必保留本文链接:https://go.coder-hub.com/76710899.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定