You can send a collection of objects that are Json serialized to a WebAPI action method, which is using encapsulation a basic OO concept. One way to do it is use the DTO pattern.
there is only one payload. but if the payload is json, you just use an array to send more than one employee. the base json can be an array or one of elements. the main restriction is max payload size.
just use an array to send more than one employee --
If I use an array[employees] to send, will I get individual response for each employee or just one response for whole array.
Thanks,
Raj
A collection the List<t> is acted upon as a whole. You do know that List<T> a collection is an array. Right? The C# List<t> collection is turned into a Json array when sent.
The List<T> class is the generic equivalent of the ArrayList class. It implements the IList<T> generic interface by using an array whose size is dynamically increased as required. You can add items to a List<T> by using the Add or AddRange methods.
for example, if we are saving employee details, can we save more than one employee in one single post.
You can use a json array to send multiple employee. I made a simple test.
public class User
{
public int Id { get; set; }
public string Name { get; set; }
public string Address { get; set; }
}
[HttpPost]
public void Post([FromBody]List<User> users)
{
}
Best Regards,
Jiadong Meng
.NET forums are moving to a new home on Microsoft Q&A, we encourage you to go to Microsoft Q&A for .NET for posting new questions and get involved today.
Member
1 Points
96 Posts
Rest API - multiple payload in a one request (post)
Apr 09, 2020 03:23 PM|rgovindarajan|LINK
Hi,
Can we send
Hi Yang,
Can we send multiple payload in a one post request of rest api?
for example, if we are saving employee details, can we save more than one employee in one single post.
can you share any sample? is there any limitations in sending payload ?
THanks,
Raj
?
for example, if we are saving employee details, can we save more than one employee in one single post.
can you share any sample? is there any limitations in sending payload ?
THanks,
Raj
Contributor
4963 Points
4213 Posts
Re: Rest API - multiple payload in a one request (post)
Apr 09, 2020 03:40 PM|DA924|LINK
You can send a collection of objects that are Json serialized to a WebAPI action method, which is using encapsulation a basic OO concept. One way to do it is use the DTO pattern.
https://en.wikipedia.org/wiki/Encapsulation_(computer_programming)
https://www.codeproject.com/Articles/1050468/Data-Transfer-Object-Design-Pattern-in-Csharp
https://docs.microsoft.com/en-us/aspnet/web-api/overview/data/using-web-api-with-entity-framework/part-5
The link may be helpful.
https://www.codeproject.com/Tips/710116/Passing-Object-Collections-to-ASP-NET-Web-API
All-Star
53101 Points
23659 Posts
Re: Rest API - multiple payload in a one request (post)
Apr 09, 2020 03:44 PM|mgebhard|LINK
Yes, Web API's model binder works with complex types.
Share your code if you need community debugging assistance. Otherwise, try going through the Web API tutorials on this site.
https://docs.microsoft.com/en-us/aspnet/web-api/overview/getting-started-with-aspnet-web-api/tutorial-your-first-web-api
All-Star
58254 Points
15675 Posts
Re: Rest API - multiple payload in a one request (post)
Apr 09, 2020 07:33 PM|bruce (sqlwork.com)|LINK
there is only one payload. but if the payload is json, you just use an array to send more than one employee. the base json can be an array or one of elements. the main restriction is max payload size.
Member
1 Points
96 Posts
Re: Rest API - multiple payload in a one request (post)
Apr 09, 2020 08:35 PM|rgovindarajan|LINK
just use an array to send more than one employee --
If I use an array[employees] to send, will I get individual response for each employee or just one response for whole array.
Thanks,
Raj
Contributor
4963 Points
4213 Posts
Re: Rest API - multiple payload in a one request (post)
Apr 09, 2020 11:17 PM|DA924|LINK
A collection the List<t> is acted upon as a whole. You do know that List<T> a collection is an array. Right? The C# List<t> collection is turned into a Json array when sent.
List<employee> = new List<employee>();
https://docs.microsoft.com/en-us/dotnet/api/system.collections.generic.list-1?view=netframework-4.8
The List<T> class is the generic equivalent of the ArrayList class. It implements the IList<T> generic interface by using an array whose size is dynamically increased as required. You can add items to a List<T> by using the Add or AddRange methods.
The linq shows what is happening.
https://www.codeproject.com/Tips/710116/Passing-Object-Collections-to-ASP-NET-Web-API
Participant
1320 Points
491 Posts
Re: Rest API - multiple payload in a one request (post)
Apr 10, 2020 02:36 AM|jiadongm|LINK
Hi rgovindarajan,
You can use a json array to send multiple employee. I made a simple test.
[HttpPost] public void Post([FromBody]List<User> users) { }
Best Regards,
Jiadong Meng