从Postman数组中获取值并传递

2ledvvac  于 2023-08-05  发布在  Postman
关注(0)|答案(1)|浏览(172)

我想得到所有的'branchId的,并设置为下一个请求的参数。不知道该怎么做。

的数据

kgsdhlau

kgsdhlau1#

我认为您只需要获得branchId和make to数组,然后应用下一个POST调用的输入。
这是服务器的演示

from flask import Flask, jsonify, request

app = Flask(__name__)
@app.route('/data/')
def print_list():
    data = {
        "profile" : {
            "contactId" :"9018398e-5c0d-43a5-99dd-0bdb8ac1b03a",
            "homeBranchId" :"07d39b5f-9f44-43ac-86c9-3bb2d6e35125"
        },
        "accountMembers" : {
            "contactId" :"485776ae-75b5-4496-9270-6af2162a955d",
            "contactDisplayName" :"bra-bra-bra",
            "contactColor" : None,
            "contactPhotoUrl" : None,
            "contactEmail" :"bra-bra-bra",
            "contactPhoneNumber" :"123-456-7890"
        },
        "branches" : [
            {
                "branchId" : "c7fd1267-a110-49f6-ab52-a36a4fe73d47",
                "branchName" :"bra-bra-bra",
                "branchPhoneNumber" :"123-456-7890",
                "branchAddress" :"bra-bra-bra",
                "branchIanaTimeZone" :"America/New_York"
            },
            {
                "branchId" : "0aa96805-a41a-4881-b0d6-0d58cc1fb68f",
                "branchName" :"bra-bra-bra",
                "branchPhoneNumber" :"123-456-7890",
                "branchAddress" :"bra-bra-bra",
                "branchIanaTimeZone" :"America/New_York"
            },
            {
                "branchId" : "4fb1aab8-b749-4893-9239-97ad880071df",
                "branchName" :"bra-bra-bra",
                "branchPhoneNumber" :"123-456-7890",
                "branchAddress" :"bra-bra-bra",
                "branchIanaTimeZone" :"America/New_York"
            }
        ]
    }
    return jsonify(data)

@app.route("/test", methods=["POST"])
def post_test():
    content_type = request.headers.get('Content-Type')
    if (content_type == 'application/json'):
        json = request.get_json()
        return jsonify(json), 201
    else:
        return 'Content-Type not supported!'

if __name__ == "__main__":
    app.run(debug=True)

字符串

安装依赖项

pip install flask

运行服务器

python server.py

通过 Postman 调用获取数据API

http://127.0.0.1:5000/data

获取结果


的数据

过滤并保存到数组变量中

Tests选项卡上

const response = pm.response.json();
// display all data
console.log(JSON.stringify(response.branches));

let onlyIds = response.branches.map(item => (item.branchId))
// display only ids after filtered
console.log(JSON.stringify(onlyIds));
pm.globals.set("onlyIds", JSON.stringify(onlyIds));



如果用这个测试脚本再次调用GET API它将显示在控制台中并将它保存到一个数组数据中。


最后,它将在POST调用的Body中使用用于数组的输入。

body的结果将显示,因为'Flash'服务器将返回与body的结果相同的输入body。

在主体的输入中访问数组数据

"branchArrayData": {{onlyIds}}


相关问题