Many situations developers have a debate on REST vs JSON-RPC. Plenty of resources available in Internet to distinguish them. This is one among them and direct how to create and use JSON-RPC with Delphi MVC Framework quickly.
REST: The Word REST is the Abbreviation of the Phase ‘Representational State Transfer’ is the Architectural Design Style and it is not a standard or Protocol. Web services that conform to the REST architectural style, called RESTful Web services, provide interoperability between computer systems on the internet. REST provides a set of architectural constraints that, when applied as a whole, emphasizes scalability of component interactions, generality of interfaces, independent deployment of components, and intermediary components to reduce interaction latency, enforce security, and encapsulate legacy systems.
RPC: A remote procedure call (RPC) is when a computer program causes a procedure (subroutine) to execute in a different address space (commonly on another computer on a shared network), which is coded as if it were a normal (local) procedure call, without the programmer explicitly coding the details for the remote interaction.
JSON-RPC : JSON-RPC is a stateless, Light-weight remote procedure call(RPC) protocol.
- It is transport agnostic, and can be used within the same process, over sockets, over http, or in many various message passing environments.
- It uses JSON(RFC 4627) as data format.
- It supports Requests( which are functions) and Notifications( which are procedures)
Request and response:
1 2 |
--> {"jsonrpc": "2.0", "method": "subtract", "params": {"minuend": 42, "subtrahend": 23}, "id": 3} <-- {"jsonrpc": "2.0", "result": 19, "id": 3} |
Notification (no response):
1 |
--> {"jsonrpc": "2.0", "method": "update", "params": [1,2,3,4,5]} |
Why JSON-RPC if i have REST?
RPC and REST are only different approaches with pros and cons and both are valuable depending on the context. The context is the key.
There are no universal solutions, you always have to think within a context and must be pragmatic when choosing a solution.
REST Vs JSON-RPC :
REST is Resource oriented while RPC is actions oriented.
REST is strongly tied with the transport protocol while RPC don’t.
REST-based APIs are great for CRUD based operations(Resources and entities). REST is not only CRUD, but things are done through mainly CRUD-based operations.
RPC can works with Sockets, http/https ftp, file system etc. Really it is not tied with any protocol, its just a way to describe an action with its parameters.
RPC-based APIs are great for actions based operations(functions and procedures)
Check this Video which demonstrates JSON-RPC with Delphi MVC Framework.
Check the sample made for JSON-RPC here.
Design. Code. Compile. Deploy.
Start Free Trial Upgrade Today
Free Delphi Community Edition Free C++Builder Community Edition
Very nice article.
More info about DMVCFramework are avialable in the book “DMVCFramework – the official guide” available in english and brasilian portuguese from leanpub :
https://leanpub.com/delphimvcframework
https://leanpub.com/delphimvcframework-br