简体   繁体   English

如何在 Postman 中将数组索引值发送到 JSON 正文中

[英]How to send array index value into JSON body in Postman

I'm chaining several requests to simulate an end to end scenario in postman.我正在链接几个请求来模拟邮递员中的端到端场景。

I have an array of values which was returned back from the first request.我有一个从第一个请求返回的值数组。 I've set the array as an environment variable so that it can be accessed by other subsequent requests in my collection.我已将数组设置为环境变量,以便我的集合中的其他后续请求可以访问它。

pm.environment.set("travArray", travArray);

The console output of the travArray looks like: travArray 的控制台输出如下所示:

0:"d4faf286-ab68-448b-87cb-ad6a7030bc57"
1:"2e21ab1f-25be-49ab-a984-db529022cf0f"
2:"9a1d942c-4048-48a7-acfc-7a1c9563c528"

Next, I'm trying to post a json request with each of these indexed values.接下来,我尝试使用这些索引值中的每一个发布一个 json 请求。 In the Body (raw) request it looks like this;在正文(原始)请求中,它看起来像这样;

    "travelers": [
    {
        "id": "{{travArray[0]}}",
        "firstName": "teaaf",
        "lastName": "fadfadsfads",
        "travelerType": "ADULT"
    },
    {
        "id": "{{travArray[1]}}",
        "firstName": "sdfsfsdf",
        "lastName": "sdfsfsd",
        "travelerType": "ADULT"
    },
    {
        "id": "{{travArray[2]}}",
        "firstName": "sdfsdf",
        "lastName": "sdfsfsdf",
        "travelerType": "ADULT"
    }
],

But using this syntax {{travArray[1]}} fails to parse the actual value when the raw request is posted to the API endpoint.但是使用此语法 {{travArray[1]}} 无法在将原始请求发布到 API 端点时解析实际值。

Interestingly, when i just use {{travArray} - without the index notation, the entire contents of the array is sent.有趣的是,当我只使用 {{travArray} - 没有索引符号时,会发送数组的全部内容。

example output:示例输出:

id:"d4faf286-ab68-448b-87cb-ad6a7030bc57,2e21ab1f-25be-49ab-a984-db529022cf0f,9a1d942c-4048-48a7-acfc-7a1c9563c528"
firstName:"teaaf"
lastName:"fadfadsfads"
travelerType:"ADULT"

Can someone tell me how to parse index value of the array in the json body request?有人能告诉我如何解析 json body 请求中数组的索引值吗?

Thanks!谢谢!

I'm new to Postman scripting, so this is my noob method of doing this.我是 Postman 脚本的新手,所以这是我的菜鸟方法。

You need to pass the array location into the test script.您需要将数组位置传递到测试脚本中。

    var jsonData = JSON.parse(responseBody);
    pm.environment.set("travArray", travArray[0].id);

You can iterate through the array by setting a variable and then using the .setNextRequest() method.您可以通过设置变量然后使用.setNextRequest()方法来遍历数组。

Set an index variable in your environment.在您的环境中设置索引变量。 Then set a for-in loop:然后设置一个 for-in 循环:

    var jsonData = JSON.parse(responseBody); // Deserialize the return
    for (let traveler in jsonData) {
        pm.setNextRequest(jsonData[traveler].id);
        // do any checks/tests with the response.
    }

Once you've processed the other request, you can add a .setNextRequest() to the script and point it back to the first request.处理完其他请求后,您可以向脚本添加 .setNextRequest() 并将其指向第一个请求。 With the .setNextRequest(null) call, Postman will stop running the iteration.通过 .setNextRequest(null) 调用,Postman 将停止运行迭代。

Good luck!祝你好运!

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM