REST APIs
What are REST APIs?
REST API is an API that uses REST (Representational State Transfer)
architectural style. The REST architectural style uses HTTP requests to
access/modify data.
REST APIs are user-friendly, which is what makes them so popular
among developers.
REST is considered as the standard protocol for web APIs.
REST APIs use the following HTTP verbs/methods to create/modify data:
HTTP Verb Equivalent CRUD Operation
POST Create data/entry
GET Read data/entry
PUT Update/Replace data
PATCH Update/Modify data
DELETE Delete data/entry
POST – Post method is used to create a new entry, say in a Database. To
be specific, it creates “subordinate resources”.
We get 201 (Created) response code when a resource has been created.
The response contains the status code and refers to the new resource
created.
GET – GET method is used to read/retrieve data. GET returns a 200
(OK) code if data is retrieved successfully, along with the response
payload in XML/JSON format.
Since, GET method will return a list of all the items/resources, and the
response payload size is a bit much, you can use paginations such as
“$top” etc. to limit the number of results.
PUT – PUT method is generally used for updating an already existing
data/resource, however if the resource does not exist, it may create a new
one.
If a new resource is created, it will return 201 status code, otherwise if a
resource is modified, it will return 200 status code.
PATCH – PATCH method makes a partial update on a resource, whereas
PUT requests modify the entire entity. It will return 200 status code once
the data is modified.
Response codes:
2XX – Successful responses
4XX – Client side Errors
5XX – Server side Errors
Response Codes Description
200(OK) Request successfully executed.
201(Created) Request successfully executed.
New resource created.
204(No content) Request successfully executed.
However, the content/body is empty.
400(Bad Request) The server cannot process the request
due to either of the following:
Malformed request syntax.
Invalid request message framing.
401(Unauthorized) The client/user is not authenticated
properly to send the request.
403(Forbidden) This time, the client is authorized,
however, they are not able to access the
resource.
404(Not Found) Often times, when the resource cannot
be located, the server throws a 404
response. 404 response code hence can
be said to indicate a data issue.
405(Method Not Allowed) This response code is observed when
request body sent to the target is of
incorrect type. Suppose, we send a
request containing a body in XML
format, but the target supports only
JSON syntax, we will get back 405 error
code.
429(Too many requests) Some APIs may have a rate-limiting
factor ,i.e, these many requests can be
sent in this much time. If this limit
exceeds, the server throws 429 errors
and does not process the remaining
requests.
500(Internal Server Error) Server encountered an unexpected
condition, and the request cannot be
executed.
502(Bad Gateway Error)
504(Gateway Error)
What is ODATA?
OData (Open Data Protocol) is REST based protocol to access
and update data.
It is built on HTTP, ATOM/XML, JSON technologies.
OData focuses on business logic while creating RESTful APIs
without the need to worry about different requests and response
headers, HTTP methods, status codes, URL and payload formats
etc.
OData must follow REST architecture as stated above.
It supports two protocols for data transfer:
XML-based ATOM format.
JSON format.