在 Node.js 中使用 API 调用清除 Cloudflare 缓存
Purging Cloudflare cache with an API call in Node.js
我希望通过其 API. More specially, the purge all files 命令清除 Cloudflare 的缓存。
但是,我将 运行 保留在 "Invalid Content-Type header, valid values are application/json,multipart/form-data" 错误消息中,尽管明确设置了 Content-Type
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
根据 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})
我希望通过其 API. More specially, the purge all files 命令清除 Cloudflare 的缓存。
但是,我将 运行 保留在 "Invalid Content-Type header, valid values are application/json,multipart/form-data" 错误消息中,尽管明确设置了 Content-Type
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
根据 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})