Sending a request without specifying the fields in the body

丶灬走出姿态 提交于 2020-05-09 06:29:29

问题


I'm trying to send several PUT request to a specific endpoint.

I am using a CSV file within the columns and values, the name of the different columns reference the different variables inside the body, do you get me?

This is the endpoint:

{{URL_API}}/products/{{sku}} --sku is the id of the product, i created that variable because i use it to pass the reference

This is the body that i use:

{
    "price":"{{price}}",
    "tax_percentage":"{{tax_percentage}}",
    "store_code":"{{store_code}}",
    "markup_top":"{{markup_top}}",
    "status":"{{status}}",
    "group_prices": [
        {
            "group":"{{class_a}}",
            "price":"{{price_a}}",
            "website":"{{website_a}}"
        }
    ]
}

I don't want to use the body anymore.. sometimes some products have more tan 1 group of prices, i mean:

    "group_prices": [
        {
            "group":"{{class_a}}",
            "price":"{{price_a}}",
            "website":"{{website_a}}"
        },
        {
            "group":"{{class_b}}",
            "price":"{{price_b}}",
            "website":"{{website_b}}"
        }

Is it possible to create something like this in the CSV file?

sku,requestBody

99RE345GT, {JSON Payload}

How should i declare the {JSON Payload}?

Can you help me?

EDIT: This is the CSV file i used:

sku,price,tax_percentage,store_code,markup_top,status,class_a,price_a,website_a 95LB645R34ER,147000,US-21,B2BUSD,1.62,1,CLASS A,700038.79,B2BUSD

I want to pass the JSON within the CSV file, i mean

sku,requestBody

95LB645R34ER,{"price":"147000","tax_percentage":"US-21","store_code":"B2BUSD","markup_top":"1.62","status":"1","group_prices": [{ "group":"CLASS A","price":"700038.79","website":"B2BUSD"}]}

Is it okay?Should i specify anything on the request body or not? I read the documentation posted in POSTMAN website but i did not find an example like this.


回答1:


As you're using JSON data as a payload, I would use a JSON file rather than a CSV file in the Collection Runner. Use this as a template and save it as data.json, it's in the correct format accepted in the Runner.

[
    {
        "sku": "95LB645R34ER",
        "payload": {
            "price": "147000",
            "tax_percentage": "US-21",
            "store_code": "B2BUSD",
            "markup_top": "1.62",
            "status": "1",
            "group_prices": [
                {
                    "group": "CLASS A",
                    "price": "700038.79",
                    "website": "B2BUSD"
                }
            ]
        }
    },
    {
        "sku": "MADEUPSKU",
        "payload": {
            "price": "99999",
            "tax_percentage": "UK-99",
            "store_code": "BLAH",
            "markup_top": "9.99",
            "status": "5",
            "group_prices": [
                {
                    "group": "CLASS B",
                    "price": "88888.79",
                    "website": "BLAH"
                }
            ]
        }
    }
]

In the pre-request Script of the request, add this code. It's creating a new local variable from the data under the payload key in the data file. The data needs to be transformed into a string so it's using JSON.stringify() to do this.

pm.variables.set("JSONpayload", JSON.stringify(pm.iterationData.get('payload'), null, 2));

In the Request Body, add this:

{{JSONpayload}}

In the Collection Runner, select the Collection you would like to run and then select the data.json file that you previously created. Run the Collection.



来源:https://stackoverflow.com/questions/60910431/sending-a-request-without-specifying-the-fields-in-the-body

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!