邮差-收集.json -如何通过传递数组使多个请求?(数组中的每一项都是每个请求的查询字符串值)

Postman - Collection.json - How to make multiple requests by passing an array? (every item in the array will be a query string value for each request)

本文关键字:请求 数组 一项 字符串 查询 何通过 json 收集 邮差      更新时间:2023-09-26

下面是我的邮差收集的json文件。我需要发出大约100个单独的请求,并测试每个请求的响应代码。在每个请求中唯一不同的是查询字符串参数UserID。

那么,我有一个类似于

的数组
var users = [123, 124, 125, 126];

我想将这个数组传递到集合中,这样我就不必为每个请求创建单独的'item'对象。有办法做到这一点吗?我认为'variables'将是一个很好的地方,但我不确定它是如何工作的。我将很高兴与一个解决方案,使用邮差UI以及。

我可以写一个小的控制台应用程序或一些javascript片段来实现相同的功能,但我想看看是否可以通过postman实现,因为我喜欢使用它。

{
    "variables": [],
    "info": {
        "name": "Test a Set of Users",
        "_postman_id": "postman-guid-id",
        "description": "",
        "schema": "https://schema.getpostman.com/json/collection/v2.0.0/collection.json"
    },
    "item": [
        {
            "name": "User1",            
            "event": [
                {
                    "listen": "test",
                    "script": {
                        "type": "text/javascript",
                        "exec": "tests['"Status code is 200'"] = responseCode.code === 200;"
                    }
                }
            ],
            "request": {
                "url": "http://localhost/myAPI/getSomething?UserId=123&isActive=true",
                "method": "GET",
                "header": [
                    {
                        "key": "Accept",
                        "value": "text/html,application/xhtml+xml,application/xml;q=0.9,image/webp,*/*;q=0.8,,application/json",
                        "description": ""
                    },  
                    //all my other request headers - they are the same for all requests
                ],
                "body": {
                    "mode": "formdata",
                    "formdata": []
                },
                "description": ""
            },
            "response": []
        },
        "item": [
        {
            "name": "User2" // everything same as above except for the querystring Parameter UserId     
        }
     ]

}

您可以通过Collection Runner实现这一点,并将您的数据作为CSV或JSON文件传入。

https://learning.postman.com/docs/running-collections/working-with-data-files/