How to generate code using Postman Collection

↘锁芯ラ 提交于 2021-01-28 04:02:44

问题


I'm aware it is possible to generate code snippets from individual Postman requests, but I can't find similar functionality for a entire collection.
I also know that there are tools like newman for running an exported collection in Postman Collection 2.1 json format.

But specifically what I'm looking for is a tool that generates bash code from a collection or from a exported collection in Postman Collection 2.1 json format. This way my co-workers who don't use Postman can replicate the API requests.

Is this option available in Postman and I'm just missing it or are there any tools that do this?


回答1:


I'm not sure I understand your question completely. But I can tell you how I export collection and then run tests.

  1. Export Collection to JSON: choose tab 'Collections' at the left tab of the Postman. Select the 3-dot menu and choose 'Export'.
  2. Export your test environment: Open 'Manage Environments'. Click download.
  3. I don't know a tool that generates bash code, I write it manually.

Write .sh file like the following to run your tests.

"newman run your_test_collection.json -r cli -e your_test_environment.json --reporter-cli-no-assertions --global-var"

Usually, I create different .sh files for different sets of tests.




回答2:


I ended using the Postman SDK and postman-code-gen to create code snippets from Postman Collections that I exported from Postman.

Not really sure why this feature isn't provided already by Postman, but I made a simple CLI tool that does it: https://github.com/arashout/postman-collection-gen

# cURL request generation
node main.js -c example_collection.json

# curl --location --request GET 'https://v7rr12wbr7.execute-api.us-west-2.amazonaws.com/prod/courses?c0=PHYS153&c1=APSC160&c2=CHEM154&version_key=1.2'
# curl --location --request GET 'https://v7rr12wbr7.execute-api.us-west-2.amazonaws.com/prod/courses?c0=PHYS153'


# How to generate other languages:
node main.js -c example_collection.json -l shell,httpie
node main.js -c example_collection.json -l Swift,URLSession


来源:https://stackoverflow.com/questions/60514286/how-to-generate-code-using-postman-collection

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!