发送请求而不指定正文中的字段 [英] Sending a request without specifying the fields in the body

查看:99
本文介绍了发送请求而不指定正文中的字段的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试向特定端点发送多个PUT请求。

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

我在列和值(不同列的名称)中使用了CSV文件引用体内的不同变量,你明白吗?

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?

这是端点:


{{URL_API}} / products / {{sku}} --sku是产品的ID,我创建了该变量,因为我使用它来传递引用

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

这是我使用的正文:

{
    "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}}"
        }
    ]
}

我不想再使用身体了。 imes一些产品的棕褐色1组价格更高,我的意思是:

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}}"
        }

是否可以在CSV文件中创建类似的内容?

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


sku,requestBody

sku,requestBody

99RE345GT,{JSON Payload}

99RE345GT, {JSON Payload}

我应该如何声明{JSON Payload}?

How should i declare the {JSON Payload}?

您能帮我吗?

编辑:
这是我使用的CSV文件:

This is the CSV file i used:


sku,价格,tax_percentage,store_code,markup_top,状态,class_a,price_a,网站_a
95LB645R34ER,147000,US-21,B2BUSD,1.62,1,CLASS A,700038.79,B2BUSD

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

我想在CSV文件中传递JSON,我的意思是

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


sku,requestBody

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}]}}

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"}]}

我可以在请求正文中指定任何内容吗?我阅读了POSTMAN网站上发布的文档,但没有找到这样的示例。

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.

推荐答案

在使用 JSON 数据作为有效负载,我将在Collection Runner中使用JSON文件而不是CSV文件。使用此模板作为模板并将其另存为 data.json ,它是Runner接受的正确格式。

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"
                }
            ]
        }
    }
]

预先请求脚本中的请求,添加此代码。它是根据数据文件中有效负载键下的数据创建一个新的 local 变量。数据需要转换为字符串,因此使用 JSON.stringify()来完成。

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}}

Collection Runner 中,选择要运行的Collection,然后选择 data.json 您先前创建的文件。运行集合。

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.

这篇关于发送请求而不指定正文中的字段的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆