我正在使用Postman调用API。我有一对夫妇调用,所以我做了一个收集,我可以运行它。我试图得到的结果保存在一个文件中的API(JSON是好的),然后每次我调用API,让文件更新。它是可能的自动保存一个响应每个API调用?你能请我怎么做吗?我已经尝试与纽曼,但我并不成功。谢谢。
9w11ddsr1#
设法在JSON文件中得到结果,虽然我必须手动更新它。在Youtube上跟随这个视频,这是.js中的代码(如果将来对任何人有帮助的话)
const newman = require('newman'); // require newman in your projectconst fs = require('fs');// call newman.run to pass `options` object and wait for callbacknewman.run({ collection: require('./name_of_postman_collection.json'), reporters: 'cli'}).on('beforeRequest', (error, data) => { if (error) { console.log(error); return; } console.log(data);}).on('request', (error, data) =>{ if (error) { console.log(error); return; } const fileName = `response ${data.item.name}.json`; const content = data.response.stream.toString(); fs.writeFile(fileName, content, function (error) { if (error) { console.error(error); } });});
const newman = require('newman'); // require newman in your project
const fs = require('fs');
// call newman.run to pass `options` object and wait for callback
newman.run({
collection: require('./name_of_postman_collection.json'),
reporters: 'cli'
}).on('beforeRequest', (error, data) => {
if (error) {
console.log(error);
return;
}
console.log(data);
})
.on('request', (error, data) =>{
const fileName = `response ${data.item.name}.json`;
const content = data.response.stream.toString();
fs.writeFile(fileName, content, function (error) {
console.error(error);
});
我仍在努力找出如何自动化的过程,以获得刷新文件每2-3小时为例。
1条答案
按热度按时间9w11ddsr1#
设法在JSON文件中得到结果,虽然我必须手动更新它。在Youtube上跟随这个视频,这是.js中的代码(如果将来对任何人有帮助的话)
我仍在努力找出如何自动化的过程,以获得刷新文件每2-3小时为例。