6

I am working on a project where I am trying to expose the function of the software. Basically I have my backend set up and was thinking of seperating the frontend from the backend code using JSON msgs. I am a little bit confused as to what is the difference between a service and an API. I know API can be build on top on services. But I have these two models in mind -- to access profile X using json-rpc

http://xyz.com/?request={"jsonrpc":"2.0","id":1,"method":"getProfile","params":{"id":"X"}}

or should it be like this using REST -

http://api.xyz.com/X

Thank You

Fox
  • 9,384
  • 13
  • 42
  • 63

2 Answers2

22

"Service" vs "API" is a pretty vague question. Often, the two terms are used interchangeably. "REST" vs "RPC" is a little easier to explain.

Usually with REST, a URL represents a specific resource such as a "user", an "account", etc.. Typically, you can create/retrieve/update/delete these resources by using the HTTP methods POST/GET/PUT/DELETE. To update the profile for user 1125 you might send the following:

POST /user/1125 HTTP/1.1
Host: wherever.com
Content-type: application/x-www-form-urlencoded

firstName=Davey&lastName=Jones&email=dj%40thebrineydeep.com

Anything you wanted to do with user 1125, you would send a request to the same URL. There are exceptions and variants of this idea, but that's the crux of it.

RPC services is more like just using a function library, which is bound to a specific URL. You might have a whole bunch of related functions all bound to the URL /services/json. Then if you wanted to change the profile for old Davey Jones, you would:

POST /services/json HTTP/1.1
Host: wherever.com
Content-type: application/json

{ "jsonrpc": "2.0",
  "id": 1,
  "method": "setProfile",
  "params": [ 1125,
    { "firstName": "Davey",
      "lastName": "Jones",
      "email": "dj@thebrineydeep.com"
    }
  ]
}

I personally like JSON-RPC better because:

  • I don't have to try and fit all of my function calls into some kind of resource-to-url mapping that might not make sense
  • We don't try to overload the HTTP response codes to indicate API errors. Every request returns a 200 response (unless there is a server error) and you know from the response body whether you got an error or not. JSON-RPC is particularly good at being explicit about error conditions.

Sometimes REST is better because:

  • Sometimes the resource-to-URL mapping fits really well
  • It is more intuitive for third parties to understand
  • It offers a simpler model for just retrieving easily-identified information

I don't think either one is any easier to code.

Edit I changed the REST example to use the more common form-encoded data instead of JSON. But of course you can specify any data format you like with REST. It isn't carved in stone.

slashingweapon
  • 11,007
  • 4
  • 31
  • 50
  • Thanks for the reply .. Does it make sense to create a "REST wrapper" around the rpc services ... like if you are accessing the user profile "user1" then the user would type - " http://xyz.com/user1 ".. This would internally call the rpc-service to get the resource with the id "user1"?? – Fox Nov 01 '12 at 00:16
  • It is doable, provided you have an easy mapping from REST<->RPC. Even so, it is extra work. You have to ask yourself what you have to gain from it. I would usually go with just one or the other. – slashingweapon Nov 01 '12 at 00:49
0

Your REST URL does not equal your JSON-RPC request.

At least it should be http://api.example.org/getProfile?id=X

There really is not much difference between the two. And your "REST" isn't a real REST unless you return a data format that reliably can express links to different URLs, e.g. XML or (X)HTML. Until this requirement is met, you should only call it "RESTful", because you really only use HTTP methods to trigger stuff and move data back and forth.

It doesn't really matter what you use - unless you know or have experience with software that supports you building one or the other solution more rapidly than the other.

Sven
  • 69,403
  • 10
  • 107
  • 109