nodeJS 请求库:使用代理键而不是 URL 发出 POST 请求

问题描述

我正在尝试使用 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]}`,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 以及成功发出 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)
        }
    })
}

相关问答

错误1:Request method ‘DELETE‘ not supported 错误还原:...
错误1:启动docker镜像时报错:Error response from daemon:...
错误1:private field ‘xxx‘ is never assigned 按Alt...
报错如下,通过源不能下载,最后警告pip需升级版本 Requirem...