Rest API Concepts
Rest API Concepts
Server Script /
Client Script API Script Database
Postman
Rest API practices
• It was derived from JavaScript, but many modern programming languages include code to generate and parse JSON-format data
Uses of JSON
• It is used while writing JavaScript based applications that includes browser extensions and websites.
• JSON format is used for serializing and transmitting structured data over network connection.
• Web services and APIs use JSON format to provide public data.
Practices of JSON
Characteristics of JSON
• JSON is easy to read and write.
• It is a lightweight text-based interchange format.
Type Description
Number Double- precision floating-point format in JavaScript
String Double-quoted Unicode with backslash escaping
Boolean True or False
Array An ordered sequence of values
Value It can be a string, a number, true or false, null etc
Object An unordered collection of key:value pairs
Whitespace Can be used between any pair of tokens
null Empty
Practices of JSON
• HTTP Client is an application library used in client side application to generate request and receive response.
• HTTP Client’s libraries varies from platform to platform.
Request Response Model
HTTP Request
HTTP Request is the first step to initiate web request/response communication. Every request is a combination of
request header, body and request URL.
GET() The GET method is used to retrieve information from the given server using a given URI. Requests using GET
should only retrieve data and should have no other effect on the data.
Head() Same as GET, but transfers the status line and header section only.
POST() A POST request is used to send data to the server, for example, customer information, file upload, etc. using
HTML forms.
PUT()
Replaces all current representations of the target resource with the uploaded content.
DELETE()
Removes all current representations of the target resource given by a URI.
Request Response Model
BACK button/Reload Harmless Data will be re-submitted (the browser should alert the user
that the data are about to be re-submitted)
Bookmarked Cannot be bookmarked
Can be bookmarked
HTTP Response:
Http response is the final step of request-response communication. Every response is a combination of
response header, body an d cookies.
200 OK The request is OK (this is the standard response for successful HTTP requests)
201 Created The request has been fulfilled, and a new resource is created
202 Accepted The request has been accepted for processing, but the processing has not been completed
203 Non-Authoritat The request has been successfully processed, but is returning information that may be from
ive Information another source
204 No Content The request has been successfully processed, but is not returning any content
205 Reset Content The request has been successfully processed, but is not returning any content, and requires that
the requester reset the document view
Request Response Model
206 Partial The server is delivering only part of the resource due to a range header sent by the client
Content
400 Bad Request
The request cannot be fulfilled due to bad syntax
401 Unauthorized The request was a legal request, but the server is refusing to respond to it.
403 Forbidden The request was a legal request, but the server is refusing to respond to it
404 Not Found The requested page could not be found but may be available again in the future
405 Method Not A request was made of a page using a request method not supported by that page
Allowed
Request Response Model
502 Bad Gateway The server was acting as a gateway or proxy and received an invalid response from the
upstream server
503 Service
Unavailable The server is currently unavailable (overloaded or down)
Request Response Model
Response Header:
∙ Provide proper http response status code.
∙ Provide proper content type, file type if any.
∙ Provide cache status if any.
∙ Authentication token should provide via response header.
∙ Only string data is allowed for response header.
∙ Provide content length if any.
∙ Provide response date and time.
∙ Follow request-response model described before.
Request Response Model
Response Body:
Response Cookies:
∙ A Restful API may send cookies just like a regular Web Application that serves HTML
∙ Avoid using response cookies as it is violate stateless principle.
∙ If required use cookie encryption, decryption and other policies
Request Response Model
Request Body:
∙ Request body should be structured in JSON Array/ Object pattern
∙ Request body hold multipart/ form-data like images, audio, video etc
∙ Request body should not hold any auth related information.
∙ Request body should associated with specific request data model, setter getter can used for this
Request Header:
∙ Request header should carry all security related information, like token, auth etc.
∙ Only string Key:Pair value is allowed for header .
∙ Request header should provide user agent information of client application.
∙ If necessary CSRF/ XSRF should provide via header.
∙ Request header should associated with middleware controller, where necessary
Authentication
Bearer Authentication
• Compact and self-contained way for securely transmitting information between parties as a JSON object.
• Information can be verified and trusted because it is digitally signed.
USES: