How to create a REST API that accepts an id array for resources

I am creating a REST API for my project. API to get the given user information:

api.com/users/[USER-ID] 

I would also like to allow the client to pass a list of user identifiers. How can I build the API so that it is RESTful and selects a list of user identifiers?

+73
rest api
Dec 27 '10 at 19:48
source share
5 answers

If you pass all your parameters to a URL, then perhaps comma-separated values ​​would be a better choice. Then you will have a URL pattern, for example:

 api.com/users?id=id1,id2,id3,id4,id5 
+75
Dec 27 2018-10-12T00:
source share
β€” -
  api.com/users?id=id1,id2,id3,id4,id5 api.com/users?ids[]=id1&ids[]=id2&ids[]=id3&ids[]=id4&ids[]=id5 

IMO, RESTful does not look above calls, however it is a fast and effective workaround (y). But the length of the url is limited by a web server like tomcat .

RESTful attempt:

 POST http://example.com/api/batchtask [ { method : "GET", headers : [..], url : "/users/id1" }, { method : "GET", headers : [..], url : "/users/id2" } ] 

The server will respond with the URI of the newly created batchtask resource.

 201 Created Location: "http://example.com/api/batchtask/1254" 

Now the client can receive a batch response or complete a task by polling

GET http://example.com/api/batchtask/1254




Here's how others tried to solve this problem:

+21
Sep 07 '15 at 10:21
source share

I find another way to do the same using @PathParam . Here is a sample code.

 @GET @Path("data/xml/{Ids}") @Produces("application/xml") public Object getData(@PathParam("zrssIds") String Ids) { System.out.println("zrssIds = " + Ids); //Here you need to use String tokenizer to make the array from the string. } 

Call the service using the following URL.

 http://localhost:8080/MyServices/resources/cm/data/xml/12,13,56,76 

Where

 http://localhost:8080/[War File Name]/[Servlet Mapping]/[Class Path]/data/xml/12,13,56,76 
+14
Aug 22 2018-11-11T00:
source share

How much do I prefer this approach: -

  api.com/users?id=id1,id2,id3,id4,id5 

The right way

  api.com/users?ids[]=id1&ids[]=id2&ids[]=id3&ids[]=id4&ids[]=id5 

or

  api.com/users?ids=id1&ids=id2&ids=id3&ids=id4&ids=id5 

This is how rack does it. Here is how php does it. Here's how node does it ...

+12
Jul 06 '15 at 15:14
source share

You can create a Rest API or a finished project using ASP.NET MVC and return the data as JSON. Controller function example:

  public JsonpResult GetUsers(string userIds) { var values = JsonConvert.DeserializeObject<List<int>>(userIds); var users = _userRepository.GetAllUsersByIds(userIds); var collection = users.Select(user => new { id = user.Id, fullname = user.FirstName +" "+ user.LastName }); var result = new { users = collection }; return this.Jsonp(result); } public IQueryable<User> GetAllUsersByIds(List<int> ids) { return _db.Users.Where(c=> ids.Contains(c.Id)); } 

Then you just call the GetUsers function through a regular AJAX function that supplies the array of identifiers (in this case I use jQuery stringify to send the array as a string and dematerialize it back to the controller, but you can just send the ints array and take it as an int array in the controller ) I created the entire Restful API using ASP.NET MVC, which returns data as a json cross domain and can be used from any application. This is, of course, if you can use ASP.NET MVC.

 function GetUsers() { var link = '<%= ResolveUrl("~")%>users?callback=?'; var userIds = []; $('#multiselect :selected').each(function (i, selected) { userIds[i] = $(selected).val(); }); $.ajax({ url: link, traditional: true, data: { 'userIds': JSON.stringify(userIds) }, dataType: "jsonp", jsonpCallback: "refreshUsers" }); } 
0
Dec 27 '10 at 20:43
source share



All Articles