1

Wanted to know the ways of handling a C# ASP.NET Webapi for a use case if we need to send a list of items.

By Get methods we will not be able to send data by JSON as Get will not accept HttpBody. The below approaches are mentioned:

  1. We can send it by comma separated however, MVC binding will not recognize that.
  2. Another way is to serialize the input to JSON object and at the server side we need to deserialize JSON object to list

What is the standard way of handling this?

Brandon Minnick
  • 13,342
  • 15
  • 65
  • 123
user2181334
  • 37
  • 2
  • 5
  • Can be marked as a duplicate of https://stackoverflow.com/questions/29927625/web-api-rest-request-list-of-items – Naga Sandeep Jan 09 '18 at 07:14
  • That thread talks about three approaches, however the above post has listed another approach such as "Other way is serialize input to JSON object in the client and at the server side we need to deserialize JSON object to list". – user2181334 Jan 09 '18 at 07:29

3 Answers3

0

It depends on which kind of data will be sent. If you want to send list of non-complex types, you can use GET;

[HttpGet]
public int GetSampleItems(int[] listOfItems)
{
    return listOfItems.Length;
}
example.com/GetSampleItems?listOfItems=1&listOfItems=2&listOfItems=3

If you want to send complex types, you should use POST instead of GET.

[HttpPost]
public int GetSampleItems(List<SampleItem> sampleItems)
{
    return sampleItems.Length;
}
public class SampleItem
{
    public string Item { get; set; }
}

And the input should be inside the request body as json format;

[
   {
      "Item":"a"
   },
   {
      "Item":"b"
   },
   {
      "Item":"c"
   }
]
lucky
  • 12,734
  • 4
  • 24
  • 46
  • Thank you. Yes we have considered above approaches. Approach1 by sending as query string parameters would not help us in terms of length of the query string and security. Also we cannot go approach2 by making it as POST , which is some what going against Rest (yes also agree that nothing is more or less restful approach) . How do you see the approach of sending serialized JSON at the client side and deserializing at the ASP.NET WebAPI side. – user2181334 Jan 09 '18 at 07:51
0

First of all, let me say that I.M.O. you should just use a post with a body, as the solution I will show you isn't very "good".

You could serialize the object as a string, send it in the url and deserialize it back in you API. You will have to have the exact same Model for the object you send on both sides.

Here's how you do that:

Frontend part

var objectModelInstance = new ObjectModel();

using(var client = new HttpClient())
{
    var path = "www.pathtowebapi.com/api/endpoint"
    var bodyString = JsonConvert.SerializeObject(objectModelInstance);
    var response = await client.GetAsync($"{path}?{bodyString}");
}

WEB API Controller

[HttpGet("{stringName}")]
public Task<IActionResult> MethodName(string stringName)
{
    var object = JsonConvert.Deserialize<ObjectModel>(stringName);
}

Although I provide a solution to your problem, I do not recommend this and just recommend using a POST.

EDIT:

You say that using POST methods is against REST, could you elaborate on that ? The first REST API i found is the twitter one ( https://developer.twitter.com/en/docs/api-reference-index ) , containing both GET & POST methods.

TanguyB
  • 1,954
  • 3
  • 21
  • 40
  • Thank you for your thoughts and effort. Approach of sending data by serialization and serialization has dependency over model object at the client side , Agree. But in case if its list of simple type there would not be dependency of model object. – user2181334 Jan 09 '18 at 10:02
  • Then you can simple do it like this with a type. No need to serialize it, just send them as parameters. – TanguyB Jan 09 '18 at 10:14
  • Also towards Rest Get method to be converted as POST for supporting list input, lots of conversations has happened. I respect each of there points , my point is as per the Http Standards of W3 ,"The POST method is used to request that the origin server accept the entity enclosed in the request as a new subordinate of the resource identified by the Request-URI in the Request-Line. " .I dont want this thread to go with GET to POST discussion point. – user2181334 Jan 09 '18 at 10:15
  • As mentioned earlier sending it by Query string has Query string length constraint. – user2181334 Jan 09 '18 at 10:24
  • Possible to send over multiple request, cut your list in pieces ? – TanguyB Jan 09 '18 at 10:34
  • what would be the draw back in sending the list of simple types by serializing at the client side and deserializing at the server side. – user2181334 Jan 09 '18 at 11:21
  • So like my example, but with simple types ? – TanguyB Jan 09 '18 at 13:46
  • Yes, we have already implemented where we are sending list of simple type s by serializing at the client side and deserializing at the server side. – user2181334 Jan 10 '18 at 05:51
  • So your problem is fixed :) ? – TanguyB Jan 10 '18 at 08:03
  • We dont have any issues, however i wanted to validate the approach we went for. – user2181334 Jan 25 '18 at 09:39
0

POST is for creating resources. If you're not creating a resource, don't use POST. GET is for reading resources. If you are reading resources use GET. Assuming you are reading resources, you can send your list as a query string parameter delimited by something, comma or otherwise, and use a custom model binder to deserialize it to whatever object you need.

Yuli Bonner
  • 1,189
  • 8
  • 12