Passing array in GET for a REST call - rest

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.

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.

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.

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!

This worked for me.
/users?ids[]=id1&ids[]=id2

/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.

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.

Related

How do I best define an REST endpoint with a verb in it?

I need to define an end-point for an action the back-end is supposed to take: format the device.
The end-point I have come up with is:
POST /device/{deviceId}/format
without any body.
This, doesn't look RESTful though. Or is it?
Is there any alternative for this ? How can I make it RESTful?
REST doesn't care what spelling you use for your URI.
https://www.merriam-webster.com/dictionary/stop
https://www.merriam-webster.com/dictionary/go
https://www.merriam-webster.com/dictionary/procrastinate
All of these identifiers work fine. The machines don't care, because they aren't trying to extract semantic information from the identifier. General-purpose clients consider the URI to be opaque (aside from certain purely mechanical concerns permitted by RFC 3986).
This, doesn't look RESTful though. Or is it?
It can be fine, there might be better choices. Caching is a very important idea in REST, and if you expect that a successful POST request will change one of the representations that the client may have cached, then you will want to think about how to communicate to the client that some cache entries need to be invalidated.
Another way of expressing the same idea; you might imagine that our API is a collection of objects. What you are trying do here is send a format message to the device. A simple spelling of this might look like
Resource(/device/{deviceId}).FORMAT()
But our problem is that FORMAT isn't part of the uniform interface currently defined by HTTP; and trying to come up with semantics for FORMAT that are consistent across all resources isn't worth the bother.
What's are alternative? It's okay to use POST.
POST serves many useful purposes in HTTP, including the general purpose of “this action isn’t worth standardizing.”
Resource(/device/{deviceId}).POST()
and that works fine. Of course, sometimes we have more than one action that isn't worth standardizing. How do we discriminate between them?
Resource(/device/{deviceId}).POST(format)
The general purpose components route the request to your post handler, then your bespoke code interrogates the request and forward it to the module in your code that specializes in handling formatting requests.

How to start with writing a RESTful service?

I am trying to create a RESTful web service that accepts JSON arguments and gives out a JSON response.
What I want is to accept HTTP requests made to my URL endpoint.
Something like,
POST /the/endpoint HTTP/1.1
Host: mywebsite.com
{"name":"yourname", "department":"your_department"}
Do a DB read at the backend and give relevant parameters like, say Manager name, salary etc as a JSON object, as the response.
What's the best way to go about it? I was thinking of using Java servlets for this? Is there a better way?
PS - I am just getting started so detailed answers or links to tutorials as to how to implement it would be much appreciated.
Thanks.
Yes you can easily do this with Servlets and some Json Libs for Marshalling /unmarshalling the Json Object to Java Object.
You can make use of Json libs like
Jackson ,
Gson etc
But you must know that REST application doesnt end with just handling the request and response , but it needs to take care of other non-functional requirements like
Authentication
Authorization
Security etc
Building this from a scratch from a Servlet is overkill and waste of time when there are ready made frameworks that these things for you
My favorite is Spring MVC 3.0
Check their project site for more details
Just to show you how easy to set up one in Spring MVC , check this below tutorial
Spring 3 REST Tutorial
Pls rate the post if it helps , Cheers.
If you want to go with Java, I suggest that you take a look at JAX-RS... And since REST is a complex topic, here is a url with tons of informations on it. http://code.google.com/p/implementing-rest/
As a complete beginner, I believe the best way to implement a (nearly) RESTful API without having to read a lot is simply to implement the API just using HTML pages and HTML forms with the back-end processing to handle them.
The rules are:
Use <a> tags to provide links to related resources. (navigation)
Use <form> tags to initiate any kind of processing operation on the server. (actions)
You can then make it properly RESTful by using progressive enhancement to add Javascript AJAX requests that perform PUT, PATCH, and DELETE instead of using POST for those three (of course, keeping POST for creating resources where the client doesn't know the resultant URI).
You can then click around and test the API in a web browser! Tools like Selenium can automate this.
If you need to provide JSON, this can be added after the API has been designed and tested, although libraries exist to process HTML or XHTML responses too, so JSON isn't necessarily required for machine readability.
if you are using php with symfony try:https://github.com/FriendsOfSymfony/FOSRestBundle this lets you create a real REST full servicer very quick.
Vogella made my day very easy when i started Web services with an super example here with eclipse screen shots ..Have a look here.

Basic JSON question

I know it might sound silly but how do I determine a website supports JSON?
I know what/how JSON use for a long time, but I do not know how could I technically know whether a
server has json support. Do I need to manually request json file to check whether it suppports or not?
Any comment would be appreciated !
You can check what is the content type of server response, usually this is
application/json
(1) "Websites" don't really serve up JSON; the notation syntax isn't really meant for page rendering, it's made for data transmission.
(2) What oftentimes serves up JSON are APIs (like those of Facebook and StackExchange). These APIs usually use HTTP GET, POST, PUT, and DELETE methods to interact with services they provide, and (sometimes optionally) transmit the bulk of the payload data in JSON format.
(3) It doesn't really make sense to ask where to get JSON. If you'd simply like to play with some JSON for educational purposes, Python, PHP, Javascript, etc. all have great built-in support. What you ought to be looking for are the services that you'd like to utilize, and whether or not they support JSON. If the service is new, or popular, and has relatively good API support, odds are it will work with JSON.
As far as I know, JOSN is just a format, like XML. You can create JSON page by hand, or use easy functions in a lot of the major coding languages.
Example: PHP does this easily with json_encode(String) json_decode(String). So, as far as my knowledge goes, every server / website can support JSON, though some might not have the updated PHP or whatever coding language you may be doing, to support easy implementation of JSON.

ASIFormDataRequest: POST both image/jpeg and application/xml with a

Given both:
NSData *jpegData;
NSString *xmlPost:
How do I construct an ASIFormDataRequest to post both the image/jpeg data and the applicaton/xml data as a multipart/mixed POST request?
I'm afraid that I'm not actually answering about your question.
Two facts.
The ASIFormDataRequest class is not designed to support 'multipart/mixed'. (at least, currently)
XML is just a plain string which can be sent via multipart/form-data too.
If your situation is forcing you have to use multipart/mixed, just ignore me. (for instance, your client don't want to modifying server program which support only multipart/mixed.)
If not, please think again once. Do you really need the multipart/mixed itself? No choice? Never 'multipart/form-data'? As I know, most server part programmers like 'multipart/form-data' more than other rarely used format because it supported by most server frameworks. So it makes their life a lot easier :)
If you really want another 'regularity' or 'efficiency' or some other specials instead of simplicity, you should consider other than HTTP. The only benefits of HTTP is stability and compatibility. (from it's well-defined specification and long history) This is most important benefits, but may not suitable for you.
I didn't try 'multipart/mixed'. Maybe it's possible someone has a simple way to do this.
But basically this is a kind of hacking job. (enabling feature which absent on original design) This should be painful. It'll be easier making your own class from the HTTP specification.
I strongly recommend you to use multipart/form-data instead of. Which is supported on the class by design. You can assume an XML source just like a plain string. Which can be sent via plain POST field value.

What is REST call and how to send a REST call?

I want to ask some questions about the REST call. I am the green for the REST call and I would like to like what is REST call and how to use the URL to send a REST call to the server. Can anyone give me some basic tutorial or link for my to reference?
Besides, if I want to send a REST call to the server, what should I do? Do I need to set something in the URL? or set something in the server? Thank you.
REST is just a software architecture style for exposing resources.
Use HTTP methods explicitly.
Be stateless.
Expose directory structure-like URIs.
Transfer XML, JavaScript Object Notation (JSON), or both.
A typical REST call to return information about customer 34456 could look like:
http://example.com/customer/34456
Have a look at the IBM tutorial for REST web services
REST is somewhat of a revival of old-school HTTP, where the actual HTTP verbs (commands) have semantic meaning. Til recently, apps that wanted to update stuff on the server would supply a form containing an 'action' variable and a bunch of data. The HTTP command would almost always be GET or POST, and would be almost irrelevant. (Though there's almost always been a proscription against using GET for operations that have side effects, in reality a lot of apps don't care about the command used.)
With REST, you might instead PUT /profiles/cHao and send an XML or JSON representation of the profile info. (Or rather, I would -- you would have to update your own profile. :) That'd involve logging in, usually through HTTP's built-in authentication mechanisms.) In the latter case, what you want to do is specified by the URL, and the request body is just the guts of the resource involved.
http://en.wikipedia.org/wiki/Representational_State_Transfer has some details.