node.jsaxiosimgix

Imgix Purge Post Request fails for unknown reason


I wanna purge an image from the imgix cache, they have a API for that (https://docs.imgix.com/setup/purging-images)

I use axios to make the request from my nodejs backend:

    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 

The problem is other than the status code 400 there is nothing else specified, I checked the image paths and tried different Content-Type headers like in their example but that also did not work.

Since I cannot get any more information out of the response I ask for help here since I do not know how to continue.


Solution

  • Checking imgix documentation, the body of your request should have a data property:

    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" } }'
    

    So, just modify your message to (adding the data property to your json object):

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