【问题标题】:Imgix Purge Post Request fails for unknown reasonImgix Purge Post 请求因未知原因而失败
【发布时间】:2021-09-01 08:16:10
【问题描述】:

我想从 imgix 缓存中清除图像,他们有一个 API (https://docs.imgix.com/setup/purging-images)

我使用 axios 从我的 nodejs 后端发出请求:

    const imgixKey = functions.config().imgix.key;

    const headers = {
        Authorization: `Bearer ${imgixKey}`,
        'Content-Type': 'application/json',
    };

    const data = JSON.stringify({
        type: 'purges',
        attributes: {
            url: href,
            source_id: 'SOsourceId',
        },
    });

    try {
        await axios.post('https://api.imgix.com/api/v1/purge', data, { headers });
        functions.logger.log('Purged Image successfully' + href);
    } catch (e) {
        functions.logger.error('Error removing image from cache ' + href + ' -> ' + e);
    }
Error removing image from cache {stackoverflowimgpath.png} -> Error: Request failed with status code 400 

问题是除了状态码 400 之外没有指定其他内容,我检查了图像路径并尝试了不同的 Content-Type 标头,就像他们的示例中一样,但也没有用。

由于我无法从回复中获得更多信息,因此我在此寻求帮助,因为我不知道如何继续。

【问题讨论】:

    标签: node.js axios imgix


    【解决方案1】:

    检查 imgix 文档,您的请求正文应该有一个数据属性:

    curl -X POST 'https://api.imgix.com/api/v1/purge' \
      --header 'Authorization: Bearer <api-key>' \
      --header 'Content-Type: application/vnd.api+json' \
      --data-raw '{ "data": { "attributes": { "url": "<url-to-purge>" }, "type": "purges" } }'
    

    因此,只需将您的消息修改为(将数据属性添加到您的 json 对象):

    const data = JSON.stringify({
      data: {
            type: 'purges',
            attributes: {
                url: href,
                source_id: 'SOsourceId',
            },
        }
    });
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2020-03-21
      • 1970-01-01
      • 1970-01-01
      • 2017-10-27
      • 2014-09-10
      相关资源
      最近更新 更多