我希望通过API清除Cloudflare的缓存。更具体地说,是purge all files命令。
但是,尽管明确设置了Content-Type
标题,但我一直在运行“Invalid Content-Type标头,有效值是application / json,multipart / form-data”错误消息使用Node.js'request包。
我错过了什么?
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);
});
输出:
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
答案 0 :(得分:6)
根据cloudfare API的documentation,您需要发送HTTP DELETE请求而不是HTTP POST请求:
修改行...
return request.post(options, callback);
...与:
return request.del(options, callback);
此外,这不是表格。您需要将JSON放在数据体中。所以,替换块......
form: {
'purge_everything': true,
}
...与:
body: JSON.stringify({'purge_everything': true})