我希望通过其 API 清除 Cloudflare 的缓存.更具体地说,purge all files命令。
但是,尽管显式设置了 Content-Type,但我一直遇到 “无效的 Content-Type header ,有效值为 application/json,multipart/form-data” 错误消息
header 与 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
最佳答案
根据documentation对于 cloudfare API,您需要发送 HTTP DELETE 请求而不是 HTTP POST 请求:
修改行...
return request.post(options, callback);
...与:
return request.del(options, callback);
此外,这不是表格。您需要将 JSON 放在数据正文中。所以,更换 block ...
form: {
'purge_everything': true,
}
...与:
body: JSON.stringify({'purge_everything': true})
关于javascript - 使用 Node.js 中的 API 调用清除 Cloudflare 缓存,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/42454175/