Reputation: 1852
I'm looking to purge Cloudflare's cache through its API. More specially, the purge all files command.
However, I keep running into the "Invalid Content-Type header, valid values are application/json,multipart/form-data" error message, despite explicitly setting the Content-Type
header with Node.js' request package.
What am I missing?
var request = require('request');
gulp.task('cfPurge', function() {
var options = {
url: 'https://api.cloudflare.com/client/v4/zones/myZoneID/purge_cache',
headers: {
'X-Auth-Email': 'email',
'X-Auth-Key': 'myAPIkey',
'Content-Type': 'application/json'
},
form: {
'purge_everything': true,
}
};
function callback(error, response, body) {
var resp = JSON.parse(body);
if (!error & response.statusCode == 200) {
console.log('CF Purge: ', resp.success);
}
else {
console.log('Error: ', resp.errors);
for (var i = 0; i < resp.errors.length; i++)
console.log(' ', resp.errors[i]);
console.log('Message: ', resp.messages);
console.log('Result: ', resp.result);
}
}
return request.post(options, callback);
});
Output:
Error: [ { code: 6003,
message: 'Invalid request headers',
error_chain: [ [Object] ] } ]
{ code: 6003,
message: 'Invalid request headers',
error_chain:
[ { code: 6105,
message: 'Invalid Content-Type header, valid values are application/json,multipart/form-data' } ] }
Message: []
Result: null
Upvotes: 2
Views: 1873
Reputation: 16629
According to the documentation for the cloudfare API, you need to send an HTTP DELETE request and not an HTTP POST request:
Modify the line...
return request.post(options, callback);
...with:
return request.del(options, callback);
Also, this is not a form. You need to put the JSON in the body of the data. So, replace the block...
form: {
'purge_everything': true,
}
...with:
body: JSON.stringify({'purge_everything': true})
Upvotes: 7