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

解决方法

暂无找到可以解决该程序问题的有效方法,小编努力寻找整理中!

如果你已经找到好的解决方法,欢迎将解决方案带上本链接一起发送给小编。

小编邮箱:dio#foxmail.com (将#修改为@)