自动从Postman调用API并在文件中生成JSON响应

wz1wpwve  于 2022-11-07  发布在  Postman
关注(0)|答案(1)|浏览(202)

我正在使用Postman调用API。我有一对夫妇调用,所以我做了一个收集,我可以运行它。我试图得到的结果保存在一个文件中的API(JSON是好的),然后每次我调用API,让文件更新。它是可能的自动保存一个响应每个API调用?你能请我怎么做吗?我已经尝试与纽曼,但我并不成功。谢谢。

9w11ddsr

9w11ddsr1#

设法在JSON文件中得到结果,虽然我必须手动更新它。在Youtube上跟随这个视频,这是.js中的代码(如果将来对任何人有帮助的话)

  1. const newman = require('newman'); // require newman in your project
  2. const fs = require('fs');
  3. // call newman.run to pass `options` object and wait for callback
  4. newman.run({
  5. collection: require('./name_of_postman_collection.json'),
  6. reporters: 'cli'
  7. }).on('beforeRequest', (error, data) => {
  8. if (error) {
  9. console.log(error);
  10. return;
  11. }
  12. console.log(data);
  13. })
  14. .on('request', (error, data) =>{
  15. if (error) {
  16. console.log(error);
  17. return;
  18. }
  19. const fileName = `response ${data.item.name}.json`;
  20. const content = data.response.stream.toString();
  21. fs.writeFile(fileName, content, function (error) {
  22. if (error) {
  23. console.error(error);
  24. }
  25. });
  26. });

我仍在努力找出如何自动化的过程,以获得刷新文件每2-3小时为例。

展开查看全部

相关问题