Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Passing array in GET for a REST call

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.

like image 623
ChrisOdney Avatar asked Aug 14 '12 01:08

ChrisOdney


People also ask

Can we pass array in Get method?

You can pass arrays to a method just like normal variables. When we pass an array to a method as an argument, actually the address of the array in the memory is passed (reference).

How do you pass the array value inside the body of POST request?

Go to Header and select Content-Type = application/json then go to body and select raw and then pass an array.

How do you send an array?

To pass an entire array to a function, only the name of the array is passed as an argument. result = calculateSum(num); However, notice the use of [] in the function definition. This informs the compiler that you are passing a one-dimensional array to the function.


2 Answers

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.

like image 86
bryanmac Avatar answered Sep 20 '22 21:09

bryanmac


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!

like image 36
sgress454 Avatar answered Sep 18 '22 21:09

sgress454