如何使用预请求脚本邮递员添加查询参数以发送请求 url?

时间:2021-04-19 17:00:05

标签: javascript api testing postman

我希望能够将查询参数添加到如下所示的预请求脚本 sendRequest url,但一直无法弄清楚如何做到这一点.... 我尝试使用不同的选项但无济于事。感谢您的帮助!

pm.sendRequest({
        url: pm.globals.get("base_url") + bankNum + "/loans/" + loan14,
        method: 'GET',
        header: {
        'Authorization': '********',
        },
    }, function (err, response) {
        console.log(response.json());
    });

1 个答案:

答案 0 :(得分:0)

pm.sendRequest(({
        url: "https://reqres.in/api/users?page=2",
        method: 'GET',
        header: {
        'Authorization': '********',
        },
    }), function (err, response) {
        console.log(response.json());
    });

您将查询作为 ?name=value 传递

您还可以通过创建普通请求然后在控制台中打印该请求来生成所需的请求。要打印请求结构,请使用:

console.log(JSON.stringify(pm.request.toJSON(),null,2))

enter image description here

现在复制:

pm.sendRequest({
  "url": {
    "protocol": "https",
    "path": [
      "api",
      "users"
    ],
    "host": [
      "reqres",
      "in"
    ],
    "query": [
      {
        "key": "page",
        "value": "2"
      }
    ],
    "variable": []
  },
  "header": [
    {
      "key": "Postman-Token",
      "value": "f3e48848-4001-4f1e-862c-b316c85dd3e7",
      "system": true
    },
    {
      "key": "Host",
      "value": "reqres.in",
      "system": true
    }
    
  ],
  "method": "GET",
  "body": {}
}
, function (err, response) {
        console.log(response.json());
    });