Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Web services - REST vs PHP JSON RPC

Tags:

json

rest

php

rpc

api

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

like image 853
Fox Avatar asked Oct 31 '12 21:10

Fox


People also ask

Which is better RPC or REST?

Understanding the Differences. The most fundamental difference between RPC and REST is that RPC was designed for actions, while REST is resource-centric. RPC executes procedures and commands with ease. Alternatively, REST is ideal for domain modeling and handling large quantities of data.

Which is easier to maintain RPC or JSON?

It would be better to choose JSON-RPC between REST and JSON-RPC to develop an API for a web application that is easier to understand. JSON-RPC is preferred because its mapping to method calls and communications can be easily understood.

Is JSON-RPC REST?

While REST supports RPC data structures, it's not the only API protocol in this category. If you like JSON, you may prefer instead to use JSON-RPC, a protocol introduced in the mid-2000s. Compared to REST and SOAP, JSON-RPC is relatively narrow in scope.


1 Answers

"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": "[email protected]"
    }
  ]
}

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.

like image 193
slashingweapon Avatar answered Oct 04 '22 08:10

slashingweapon