【问题标题】:nodeJS request library: making POST request with surrogate key instead of URLnodeJS 请求库:使用代理键而不是 URL 发出 POST 请求
【发布时间】:2021-03-11 19:21:38
【问题描述】:

我正在尝试使用 nodeJS 请求库发出 POST 请求以清除与某个 surrogate key via Fastly API 关联的内容。 POST 请求看起来像这样:

POST /service/SU1Z0isxPaozGVKXdv0eY/purge
Content-Type: application/json
Accept: application/json
Fastly-Key: YOUR_FASTLY_TOKEN
Fastly-Soft-Purge: 1
Surrogate-Key: key_1 key_2 key_3

我尝试在 node.JS 中以两种不同的方式做到这一点。

第一个:

    // perform request to purge
    request({
        method: `POST`,
        url: `/service/${fastly_service_id}/purge${surrogateKeyArray[i]}`, 
        headers: headers,
    }, function(err, response, body) {
        // url was not valid to purge
        if (err) {
            console.log("is there an err???")
            console.log(err)
        }
    })
    }

我明白了,Error: Invalid URI: /service/<fastly_Service_id>/purge/<surrogate_key>

我通过curl -s -I -H "Fastly-Debug: 1" <URL corresponding to surrogate key> | grep surrogate-key仔细检查了我的代理键

它返回我的代码中使用的相同代理键。

在第二次尝试中,我尝试了:

    // perform request to purge
    request({
        method: `POST /service/${fastly_service_id}/purge${surrogateKeyArray[i]}`,
        headers: headers,
    }, function(err, response, body) {
        // url was not valid to purge
        if (err) {
            console.log("is there an err???")
            console.log(err)
        }
    })
    }

我得到了错误,Error: options.uri is a required argument

【问题讨论】:

    标签: node.js request http-post fastly


    【解决方案1】:

    我不熟悉节点以及成功发出 HTTP 请求所涉及的代码,但从我所看到的您提供的代码来看,错误似乎与您没有提供完全合格的事实有关域(例如,您在路径前缺少https://api.fastly.com)。

    当然,除非这是在您的代码中的其他地方配置的,并且在此处不可见。

    还要确保在 /purge${surrogateKeyArray[i]} 之间包含一个 / 分隔符(我在下面的示例代码中展示了这一点)。

    因此,考虑到这一点,我建议尝试:

    request({
        method: `POST`,
        url: `https://api.fastly.com/service/${fastly_service_id}/purge/${surrogateKeyArray[i]}`, 
        headers: headers,
        }, function(err, response, body) {
            if (err) {
                console.log(err)
            }
        })
    }
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2017-06-28
      • 2018-08-19
      • 2021-02-26
      • 2015-06-11
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多