Michael James
Michael James

Reputation: 61

Need to determine how to send REST Parameter for list

I am trying to test out a REST service I created to add a entry to the database. The class that defines this looks like this:

public string OrderNumber
public string FirstName
public string LastName
Public List<Items> Items

The "Items" class is defined as:

public string Material
public int Amount
public decimal Quantity

My question is how to format a REST "POST" to send the List Items over?

Below is what I tried to use to send over the list of items but it fails and the items do not come over.

http://mytesturl.com/CreateOrder?OrderNumber=ABC123&FirstName=Mark&LastName=Jones&Items=Items[]{"Material": "IPHONE", "Quantity": 1, "Amount": 45.50} 

Upvotes: 0

Views: 40

Answers (2)

Michael James
Michael James

Reputation: 61

Thanks everyone, I would have prefaced that this was my first time trying to use Postman for testing REST calls. I didnt realize it supported sending JSON requests. Once I used JSON request, everything worked perfectly.

Here is the JSON request I used if anyone is interested:

{
"OrderNumber":"ABC123",
"FirstName":"Mark",
"LastName":"Jones",
"Items":
[
{
"Material":"IPHONE",
"Quantity":1,
"Amount":59.8
},
{
"Material":"SAMSUNG",
"Quantity":29,
"Amount":65.27
}
]
}

Upvotes: 0

Rahul
Rahul

Reputation: 77876

First of all you should pass that data as form body in the request and not with query string. Thus in your API method should use [FromBody]. In your current case that List<Item> would become a JSON array and thus send it like

&Items=[
        {"Material": "IPHONE", "Quantity": 1, "Amount": 45.50},
        {"Material": "Samsung", "Quantity": 1, "Amount": 35.50}
       ]

Upvotes: 1

Related Questions