简体   繁体   English

Postman:发送嵌套JSON object

[英]Postman: sending nested JSON object

I am using ASP.NET Web API: http://xyzdomain.com:16845/api/returns/returns我正在使用 ASP.NET Web API: http://xyzdomain.com:16845/api/returns/returns

How do I send a POST request to the endpoint using Postman Chrome extension, given Items is a collection:如何使用 Postman Chrome 扩展向端点发送 POST 请求,给定Items是一个集合:

[
  {
    "Items": [
      {
        "sku": "9257",
        "Price": "100",
        "Quantity": "500",
        "DiscountPercent": "1",
        "backordered": "2"
      }
    ],
    "order_id": "F429768865001",
    "status_code": "Shelf",
    "Exception": "no error"
  }
]

Send it as raw data and set the type to application/json将其作为原始数据发送并将类型设置为application/json

在此处输入图片说明

To post a nested object with the key-value interface you can use a similar method to sending arrays.要使用键值接口发布嵌套对象,您可以使用类似于发送数组的方法。 Pass an object key in square brackets after the object index.在对象索引后传递方括号中的对象键。

使用 Postman 传递嵌套项目

"Items": [
      {
        "sku": "9257",
        "Price": "100"
      }
 ]

I got it working using the Raw data option in postman, as you can see in the screen shot我在邮递员中使用原始数据选项让它工作,正如你在屏幕截图中看到的

在此处输入图片说明

This is a combination of the above, because I had to read several posts to understand.这是上面的组合,因为我必须阅读几篇文章才能理解。

  1. In the Headers , add the following key-values:Headers 中,添加以下键值:
    1. Content-Type to application/json application/json Content-Type
    2. and Accept to application/jsonAccept application/json

在此处输入图片说明

  1. In the Body :身体
    1. change the type to "raw"将类型更改为“原始”
    2. confirm "JSON (application/json)" is the text type确认“JSON (application/json)”是文本类型
    3. put the nested property there: { "Obj1" : { "key1" : "val1" } }将嵌套属性放在那里: { "Obj1" : { "key1" : "val1" } }

在此处输入图片说明

Hope this helps!希望这可以帮助!

The key-value pair can take advanced inputs.键值对可以采用高级输入。

Ex.前任。

在此处输入图片说明

Simply add these parameters : In the header option of the request, add Content-Type:application/json只需添加这些参数:在请求的标头选项中,添加Content-Type:application/json

标题内容类型邮递员 json

and in the body , select Raw format and put your json params like {'guid':'61791957-81A3-4264-8F32-49BCFB4544D8'}并在正文中,选择原始格式并将您的 json 参数设置为{'guid':'61791957-81A3-4264-8F32-49BCFB4544D8'}

json 请求邮递员

I've found the solution on http://www.iminfo.in/post/post-json-postman-rest-client-chrome我在http://www.iminfo.in/post/post-json-postman-rest-client-chrome上找到了解决方案

Select the body tab and select application/json in the Content-Type drop-down and add a body like this:选择body选项卡并在 Content-Type 下拉列表中选择application/json并添加如下所示的正文:

{
  "Username":"ABC",
  "Password":"ABC"
}

在此处输入图片说明

Just wanted to add one more problem that some people might find on top of all the other answers.只是想再添加一个问题,有些人可能会在所有其他答案之上找到这些问题。 Sending JSON object using RAW data and setting the type to application/json is what is to be done as has been mentioned above.如上所述,使用 RAW 数据发送 JSON 对象并将类型设置为application/json

Even though I had done so, I got error in the POSTMAN request, it was because I accidentally forgot to create a default constructor for both child class.即使我这样做了,我还是在 POSTMAN 请求中出错,这是因为我不小心忘记为两个子类创建默认构造函数。

Say if I had to send a JSON of format:假设我必须发送格式的 JSON:

{
 "firstname" : "John",
 "lastname" : "Doe",
 "book":{
   "name":"Some Book",
   "price":12.2
  }
}

Then just make sure you create a default constructor for Book class.然后只需确保为 Book 类创建一个默认构造函数。

I know this is a simple and uncommon error, but did certainly help me.我知道这是一个简单且不常见的错误,但确实对我有帮助。

Best way to do that:最好的方法是:

  1. In the Headers, add the following key-values:在标题中,添加以下键值:

     Content-Type to applications/json Accept to applications/json
  2. Under body, click raw and dropdown type to application/json在 body 下,单击raw和下拉类型到application/json

Also PFA for the same同样的 PFA

在此处输入图片说明

在此处输入图片说明

For a nested Json(example below), you can form a query using postman as shown below.对于嵌套的 Json(下面的示例),您可以使用 postman 形成查询,如下所示。

{
    "Items": {
        "sku": "10 Units",
        "Price": "20 Rs"
    },
    "Characteristics": {
        "color": "blue",
        "weight": "2 lb"
    }
}

在此处输入图片说明

Just for those who want to send a nested JSON object with form-data as content type.仅适用于那些想要发送带有表单数据作为内容类型的嵌套 JSON 对象的人。

I want to send nested custom_field in below { "description": "Details about the issue...", "subject": "Support Needed...", "type": "Others", "email": "tom@outerspace.com", "priority": 1, "status": 2, **"custom_fields" : { "cf_type_of_issue" : "Complaint" }**, "cc_emails": ["ram@freshdesk.com","diana@freshdesk.com"] }我想在下面发送嵌套的 custom_field { "description": "Details about the issue...", "subject": "Support Needed...", "type": "Others", "email": "tom@outerspace.com", "priority": 1, "status": 2, **"custom_fields" : { "cf_type_of_issue" : "Complaint" }**, "cc_emails": ["ram@freshdesk.com","diana@freshdesk.com"] }

在此处输入图片说明

In the Params I have added model.Email and model.Password, work for me well.在参数中,我添加了 model.Email 和 model.Password,对我来说效果很好。 Thanks for the question.谢谢你的提问。 I tried the same thing in headers did not work.我在标题中尝试了同样的事情没有奏效。 But it worked on Body with form-data and x-www-form-urlencoded.但它使用 form-data 和 x-www-form-urlencoded 处理 Body。

Postman version 6.4.4邮递员版本 6.4.4

在此处输入图片说明

we can send nested json like the following format我们可以像以下格式一样发送嵌套的json

address[city] address[state]地址[城市] 地址[州]

If you want to send it as key-value Query Param via GET, you can paste your json in the inputtext as value, and then select this text, right click on it and click "EncodeUriComponent" :如果您想通过 GET 将其作为键值查询参数发送,您可以将您的 json 作为值粘贴到 inputtext 中,然后选择此文本,右键单击它并单击“EncodeUriComponent”

在此处输入图片说明

If you want to post single nested dictionary lets say:如果你想发布单个嵌套字典,可以说:

Wrong:错误的:

{

 parent_key: { child_key: child_value }

}

You need to convert it to:您需要将其转换为:

Correct正确的

{

 parent_key.child_key: child_value

}

And if you wanted to post list of dictionaries lets say:如果您想发布词典列表,可以说:

Wrong:错误的:

{

 parent_key: [

    { child_key: child_value },
    { child_key: child_value_02 }
  ]

}

You need to convert it to:您需要将其转换为:

Correct正确的

{

 parent_key[0]child_key: child_value
 parent_key[1]child_key: child_value_02

}

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

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