102

I have a url to fetch appointments for a user like this:

/user/:userId/appointments

How should the url look like if I want to get appointments for multiple users?

should it be:

/appointments?users=1d1,1d2..

Thanks, Chris.

6 Answers 6

114

Collections are a resource so /appointments is fine as the resource.

Collections also typically offer filters via the querystring which is essentially what users=id1,id2... is.

So,

/appointments?users=id1,id2 

is fine as a filtered RESTful resource.

Sign up to request clarification or add additional context in comments.

2 Comments

What if you have 30 key value pairs you want to pass?
Also you have to consider some limitations of browsers and frameworks for query string length: stackoverflow.com/questions/812925/…
27

Another way of doing that, which can make sense depending on your server architecture/framework of choice, is to repeat the same argument over and over again. Something like this:

/appointments?users=id1&users=id2

In this case I recommend using the parameter name in singular:

/appointments?user=id1&user=id2

This is supported natively by frameworks such as Jersey (for Java). Take a look on this question for more details.

3 Comments

also in golang i found 2 libraries is doing it like your example not with comma separated google/go-querystring and gorilla/schema
This is the way Swagger or OpenAPI Spec describes it, if you define a multiselect picklist as a query parameter. I vote for this solution!
This is also the way how Node.js would decode the query as an array. querystring.parse
17

I think it's a better practice to serialize your REST call parameters, usually by JSON-encoding them:

/appointments?users=[id1,id2]

or even:

/appointments?params={users:[id1,id2]}

Then you un-encode them on the server. This is going to give you more flexibility in the long run.

Just make sure to URLEncode the params as well before you send them!

2 Comments

?{users:[id1,id2]} doesn't follow querystring params conventions of ?key1=val2&key2=val2.
Also, do you have example of major services offering serialized objects in querystring filters? From what I've seen most offer simple filters of comma delimited options or query formats like OData
12

This worked for me.

/users?ids[]=id1&ids[]=id2

1 Comment

This method also has the added benefit of allowing comma's to be present in the array values too, not that you would have that in an id value, but there may be some edge case I suppose.
2
/appointments?users=1d1,1d2.. 

is fine. It's pretty much your only sensible option since you can't pass in a body with a GET.

Comments

-7

Instead of using http GET, use http POST. And JSON. Or XML

This is how your request stream to the server would look like.

POST /appointments HTTP/1.0
Content-Type: application/json
Content-Length: (calculated by your utility)

{users: [user:{id:id1}, user:{id:id2}]}

Or in XML,

POST /appointments HTTP/1.0
Content-Type: application/json
Content-Length: (calculated by your utility)

<users><user id='id1'/><user id='id2'/></users>

You could certainly continue using GET as you have proposed, as it is certainly simpler.

/appointments?users=1d1,1d2

Which means you would have to keep your data structures very simple.

However, if/when your data structure gets more complex, http GET and without JSON, your programming and ability to recognise the data gets very difficult.

Therefore,unless you could keep your data structure simple, I urge you adopt a data transfer framework. If your requests are browser based, the industry usual practice is JSON. If your requests are server-server, than XML is the most convenient framework.

JQuery

If your client is a browser and you are not using GWT, you should consider using jquery REST. Google on RESTful services with jQuery.

10 Comments

I dont think this is the correct way to go about it. You are GETting a resource not POSTing a new one.
I don't think you understand the http GET/POST uses. They do not conform to the English dictionary meaning for those words. POST is when trying to GET but with the arguments not placed not in the url but in the io stream.
You CAN use a POST like this but it is not idiomatic - "By design, the POST request method requests that a web server accepts and stores the data enclosed in the body of the request message." en.wikipedia.org/wiki/POST_(HTTP)
By historical use in HTML forms, and therefore not the design of REST which came later, POST has been used to not expose request parameters, and is still used that way today. And is the recommended practice. Regardless what wikipedia says.
RFC 1945 (1996, HTTP/1.0 spec) tells to use POST "to create a subordinate of the resource". Later, RFC 7231 (2014, HTTP/1.1: Semantics and Content spec) made it more vague by stating "target resource process ... request according to the resource's own specific semantics". So, technically RFCs does not restrict using POST to get some data from the server, but discourage it (in further paragraphs I won't cite here for brevity).
|

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.