没有不值得去解决的问题,也没有不值得去学习的技术!

Set and Use Variables in Postman: Update Values in One Place Instead of Many

Make a GET request in Postman: {{channel_pub_api_host_info}}/qq{{channel_pub_api_base_url}}/qq-cw-apps?group_id=015ce30b116ce86058fa6ab4fea4ac63 , the actual request URL: http://api.channel-pub-api.localhost/qq/v1/qq-cw-apps?group_id=015ce30b116ce86058fa6ab4fea4ac63 , the environment variables have been replaced, meeting expectations.

1. Open the URL for variables and environment variables: https://learning.postman.com/docs/postman/variables-and-environments/variables/ . Variables and environment variables. Using variables allows you to store and reuse values in requests and scripts. As shown in Figure 1

Open the URL for variables and environment variables: https://learning.postman.com/docs/postman/variables-and-environments/variables/ . Variables and environment variables. Using variables allows you to store and reuse values in requests and scripts.
Figure 1

2. Click Environment quick look (the eye button) in the upper right corner of Postman, then click the Add button next to Environment. As shown in Figure 2

Click Environment quick look (the eye button) in the upper right corner of Postman, then click the Add button next to Environment.
Figure 2

3. Add a variable named channel_pub_api_host_info and set its initial value to http://api.channel-pub-api.localhost , add a variable named channel_pub_api_base_url and set its initial value to /v1 , then click the Add button. As shown in Figure 3

Add a variable named channel_pub_api_host_info and set its initial value to http://api.channel-pub-api.localhost , add a variable named channel_pub_api_base_url and set its initial value to /v1 , then click the Add button.
Figure 3

4. Make a GET request in Postman: http://api.channel-pub-api.localhost/qq/v1/qq-cw-apps?group_id=015ce30b116ce86058fa6ab4fea4ac63 , as shown in Figure 4

Make a GET request in Postman: http://api.channel-pub-api.localhost/qq/v1/qq-cw-apps?group_id=015ce30b116ce86058fa6ab4fea4ac63
Figure 4

5. Replace http://api.channel-pub-api.localhost with: {{channel_pub_api_host_info}}, replace /v1 with: {{channel_pub_api_base_url}}, click the No Environment dropdown list in the upper right corner of Postman, and select 渠道发布接口 ( 本地环境 ). Hover over the variable name: {{channel_pub_api_host_info}}, and you can see its value is: http://api.channel-pub-api.localhost . As shown in Figure 5

Replace http://api.channel-pub-api.localhost with: {{channel_pub_api_host_info}}, replace /v1 with: {{channel_pub_api_base_url}}, click the No Environment dropdown list in the upper right corner of Postman, and select 渠道发布接口 ( 本地环境 ). Hover over the variable name: {{channel_pub_api_host_info}}, and you can see its value is: http://api.channel-pub-api.localhost .
Figure 5

6. Make a GET request in Postman: {{channel_pub_api_host_info}}/qq{{channel_pub_api_base_url}}/qq-cw-apps?group_id=015ce30b116ce86058fa6ab4fea4ac63 , the actual request URL: http:///qq/qq-cw-apps?group_id=015ce30b116ce86058fa6ab4fea4ac63 , the environment variables are not replaced, which does not meet expectations. As shown in Figure 6

Make a GET request in Postman: {{channel_pub_api_host_info}}/qq{{channel_pub_api_base_url}}/qq-cw-apps?group_id=015ce30b116ce86058fa6ab4fea4ac63 , the actual request URL: http:///qq/qq-cw-apps?group_id=015ce30b116ce86058fa6ab4fea4ac63 , the environment variables are not replaced, which does not meet expectations.
Figure 6

7. Reset the environment variables by setting the CURRENT VALUE, as shown in Figure 7

Reset the environment variables by setting the CURRENT VALUE
Figure 7

8. Make a GET request in Postman: {{channel_pub_api_host_info}}/qq{{channel_pub_api_base_url}}/qq-cw-apps?group_id=015ce30b116ce86058fa6ab4fea4ac63 , the actual request URL: http://api.channel-pub-api.localhost/qq/v1/qq-cw-apps?group_id=015ce30b116ce86058fa6ab4fea4ac63 , the environment variables have been replaced, meeting expectations. As shown in Figure 8

Make a GET request in Postman: {{channel_pub_api_host_info}}/qq{{channel_pub_api_base_url}}/qq-cw-apps?group_id=015ce30b116ce86058fa6ab4fea4ac63 , the actual request URL: http://api.channel-pub-api.localhost/qq/v1/qq-cw-apps?group_id=015ce30b116ce86058fa6ab4fea4ac63 , the environment variables have been replaced, meeting expectations.
Figure 8

9. At this point, the request URL is for the local environment. To prepare to request the development environment URL, add a new set of environment variables: 渠道发布接口 ( 开发环境 ), as shown in Figure 9

At this point, the request URL is for the local environment. To prepare to request the development environment URL, add a new set of environment variables: 渠道发布接口 ( 开发环境 )
Figure 9

10. Click the No Environment dropdown list in the upper right corner of Postman, and select 渠道发布接口 ( 开发环境 ). Make a GET request in Postman: {{channel_pub_api_host_info}}/qq{{channel_pub_api_base_url}}/qq-cw-apps?group_id=015ce30b116ce86058fa6ab4fea4ac63 , the actual request URL: https://api.channel-pub.wjdev.chinamcloud.cn/qq/v1/qq-cw-apps?group_id=015ce30b116ce86058fa6ab4fea4ac63 , the environment variables have been replaced, meeting expectations. Note: The response uses http instead of https because of the development environment itself, which is unrelated to Postman. As shown in Figure 10

Click the No Environment dropdown list in the upper right corner of Postman, and select 渠道发布接口 ( 开发环境 ). Make a GET request in Postman: {{channel_pub_api_host_info}}/qq{{channel_pub_api_base_url}}/qq-cw-apps?group_id=015ce30b116ce86058fa6ab4fea4ac63 , the actual request URL: https://api.channel-pub.wjdev.chinamcloud.cn/qq/v1/qq-cw-apps?group_id=015ce30b116ce86058fa6ab4fea4ac63 , the environment variables have been replaced, meeting expectations. Note: The response uses http instead of https because of the development environment itself, which is unrelated to Postman.
Figure 10

11. Click Environment quick look (the eye button) in the upper right corner of Postman, then click the Edit button next to Globals. Add a variable named group_id, and set its initial value and current value to 015ce30b116ce86058fa6ab4fea4ac63 , as shown in Figure 11

Click Environment quick look (the eye button) in the upper right corner of Postman, then click the Edit button next to Globals. Add a variable named group_id, and set its initial value and current value to 015ce30b116ce86058fa6ab4fea4ac63
Figure 11

12. Make a GET request in Postman: {{channel_pub_api_host_info}}/qq{{channel_pub_api_base_url}}/qq-cw-apps?group_id={{group_id}} , the actual request URL: http://api.channel-pub-api.localhost/qq/v1/qq-cw-apps?group_id=015ce30b116ce86058fa6ab4fea4ac63 , the environment variables have been replaced, meeting expectations. Switching to the 2 environments respectively, both will automatically replace the value of {{group_id}} with: 015ce30b116ce86058fa6ab4fea4ac63. That is, global variables can be used across all environments. As shown in Figure 12

Make a GET request in Postman: {{channel_pub_api_host_info}}/qq{{channel_pub_api_base_url}}/qq-cw-apps?group_id={{group_id}} , the actual request URL: http://api.channel-pub-api.localhost/qq/v1/qq-cw-apps?group_id=015ce30b116ce86058fa6ab4fea4ac63 , the environment variables have been replaced, meeting expectations. Switching to the 2 environments respectively, both will automatically replace the value of {{group_id}} with: 015ce30b116ce86058fa6ab4fea4ac63. That is, global variables can be used across all environments.
Figure 12

13. If variables with the same name are declared in two different scopes, the value stored in the variable with the narrowest scope will be used. At this stage, only the scopes Global and Environment are used. Global variables allow you to access data across collections, requests, test scripts, and environments. Environment variables allow you to tailor processing for different environments, such as local development versus testing or production. Only one environment can be active at a time. As shown in Figure 13

If variables with the same name are declared in two different scopes, the value stored in the variable with the narrowest scope will be used. At this stage, only the scopes Global and Environment are used. Global variables allow you to access data across collections, requests, test scripts, and environments. Environment variables allow you to tailor processing for different environments, such as local development versus testing or production. Only one environment can be active at a time.
Figure 13

14. In the local environment, add a variable named group_id, and set its initial value and current value to 5ea4502c6f2f11eaa9874577d836ad48 , click the No Environment dropdown list in the upper right corner of Postman, and select 渠道发布接口 ( 本地环境 ). Make a GET request in Postman: {{channel_pub_api_host_info}}/qq{{channel_pub_api_base_url}}/qq-cw-apps?group_id={{group_id}} , the actual request URL: http://api.channel-pub-api.localhost/qq/v1/qq-cw-apps?group_id=5ea4502c6f2f11eaa9874577d836ad48 , the environment variables have been replaced, meeting expectations. That is, the environment variable overrides the global variable {{group_id}}. As shown in Figure 14

In the local environment, add a variable named group_id, and set its initial value and current value to 5ea4502c6f2f11eaa9874577d836ad48 , click the No Environment dropdown list in the upper right corner of Postman, and select 渠道发布接口 ( 本地环境 ). Make a GET request in Postman: {{channel_pub_api_host_info}}/qq{{channel_pub_api_base_url}}/qq-cw-apps?group_id={{group_id}} , the actual request URL: http://api.channel-pub-api.localhost/qq/v1/qq-cw-apps?group_id=5ea4502c6f2f11eaa9874577d836ad48 , the environment variables have been replaced, meeting expectations. That is, the environment variable overrides the global variable {{group_id}}.
Figure 14

关于作者 · 持续学习与技术实践

我是一名拥有 15 年以上开发经验的技术从业者,自 2013 年起持续运营个人技术博客,记录工作与个人项目中遇到的真实问题,以及解决问题过程中的探索、实践和思考。

除了技术写作,我也在持续开发和维护自己的独立项目,探索新技术和新工具在实际场景中的应用。我始终相信,没有不值得去解决的问题,也没有不值得去学习的技术。

如果这篇文章对你有所帮助,欢迎浏览博客中的其他文章,也欢迎交流技术经验与想法。

关于我  ·  GitHub  ·  邮件联系