4

I need to define a REST API which is supposed to take the object's unique identifier and return back the content. The content is retrieved from the database and is of JSON type. So, I have a REST URL like this -

GET /data/{typename}/{objectid}

This would return the entire object content.

However, the content of the object could be large in size and so caller may like to specify only some or few of the properties to be sent as a response. The natural thought that comes to me is to add a BODY to the GET API where user could specify the list of property names on that object to be retrieved. But on doing some further research, it appears that a GET API with BODY is not recommended. The other option that I can think of is to pass the property names in query string -

GET /data/{typename}/{objectid}?property=prop1&property=prop2...

But the list could easily become large.

Any suggestion on how should my API look like? Do I have to use POST?

TylerH
  • 20,799
  • 66
  • 75
  • 101
tyrion
  • 714
  • 2
  • 7
  • 27
  • Would excluding properties make the URL shorter in your case? For example `?excludes=thisProp,thatProp,anotherProp`? – sp00m Feb 10 '17 at 09:56
  • Possible duplicate of [RESTful API, what if the query string isn't long enough?](http://stackoverflow.com/questions/41741356/restful-api-what-if-the-query-string-isnt-long-enough) – sp00m Feb 10 '17 at 09:57
  • The concern is not the length of query string here. The concern is to understand how to pass additional parameters that would affect the response of a GET API. – tyrion Feb 10 '17 at 10:22
  • Does this answer your question? [How to design a REST API to request only specific fields from a resource](https://stackoverflow.com/questions/18235546/how-to-design-a-rest-api-to-request-only-specific-fields-from-a-resource) – TylerH Jul 19 '23 at 19:12

2 Answers2

4

Technically, using POST will work but is not preferred. If you are reading, use GET. Facebook, for example, has a similar use case where the /me endpoint has many filters, and the call is GET.

The final URL would be, /me?fields=id,name,about,age_range,devices,currency,education

You can try it yourself from here, https://developers.facebook.com/tools/explorer/145634995501895/?method=GET&path=me%3Ffields%3Did%2Cname%2Cabout%2Cage_range%2Cdevices%2Ccurrency%2Ceducation&version=v2.8

I recommend reading more about GraphAPI https://developers.facebook.com/docs/graph-api/overview/ and GraphQL https://graphql.org/

Amr Eladawy
  • 4,193
  • 7
  • 34
  • 52
  • Thanks for the answer. I do feel POST may not be the right way for it. The facebook example gives me a confidence that query string may be the right way to do it. However I do not have a facebook login so couldn't see it in action, but I get the gist. – tyrion Feb 10 '17 at 10:26
  • 1
    Altough you are technically correct that for retrieving data a `GET` request is most correct, you really do have a hard limit on your query string length that can be a problem, like for example using datatables. https://datatables.net/examples/server_side/post.html The generated query string hits the limit very quickly – online Thomas Feb 10 '17 at 10:27
  • Do you think ',' as delimter of field names would be a concern? – tyrion Feb 10 '17 at 10:28
  • Not at all, when you get the parameter from the GET request, you will get the whole string. You can then split by comma to get the fields' names the client want to read. I think Facebook API i very close to what you want. – Amr Eladawy Feb 10 '17 at 19:01
0

I would recommend using POST, because GET has a length limit. What is the maximum length of a URL in different browsers?

otherwise you could make your query string look like this (for array) so close :)

GET /data/{typename}/{objectid}?property[]=prop1&property[]=prop2...
Community
  • 1
  • 1
online Thomas
  • 8,864
  • 6
  • 44
  • 85