Understanding APIs and REST API

API (Application Programming Interface)

Example:

Restaurant Analogy:
Provides Layer of Security
The Modern APIs

Over the years, what an API is has often described any sort of generic connectivity interface to an application. More recently, however, the modern API has taken on some characteristics that make them extraordinarily valuable and useful:

REST ( REpresentational State Transfer)

rest_api.png

Resources:
Uniform Resource Identifier (URI):
REPRESENTATIONAL STATE TRANSFER
SEPARATION OF CLIENT AND SERVER
STATELESSNESS

COMMUNICATION BETWEEN CLIENT AND SERVER

In the REST architecture, clients send requests to retrieve or modify resources, and servers send responses to these requests.

MAKING REQUESTS

REST requires that a client make a request to the server in order to retrieve or modify data on the server. A request generally consists of:

HTTP VERBS

There are 4 basic HTTP verbs we use in requests to interact with resources in a REST system:

HEADERS AND ACCEPT PARAMETERS

MIME Types are used to specify the content types in the Accept field, consist of a type and a subtype.

They are separated by a slash (/).

  • text - text/html, text/css, text/plain(default)
  • image — image/png, image/jpeg, image/gif
  • audio — audio/wav, image/mpeg
  • video — video/mp4, video/ogg
  • application — application/json, application/pdf, application/xml, application/octet-stream
GET /articles/23
Accept: text/html, application/xhtml
PATHS

SENDING RESPONSES

CONTENT TYPES

Example: when a client is accessing a resource with id 23 in an articles resource with this GET Request:

GET /articles/23 HTTP/1.1
Accept: text/html, application/xhtml

The server might send back the content with the response header:

HTTP/1.1 200 (OK)
Content-Type: text/html

This would signify that the content requested is being returning in the response body with a content-type of text/html, which the client said it would be able to accept.

RESPONSE CODES
Status code	                   Meaning
------------------------------------------------------------------------------------------------------
200 (OK)	       Successful HTTP requests.
201 (CREATED)	   An HTTP request that resulted in an item being successfully created.
204 (NO CONTENT)   Successful HTTP requests, where nothing is being returned in the response body.
400 (BAD REQUEST)  Request can't be processed coz of bad request syntax, excessive size, or another client error.
403 (FORBIDDEN)	   The client does not have permission to access this resource.
404 (NOT FOUND)	   The resource could not be found at this time. It is possible it was deleted, or does not exist yet.
500 (INTERNAL SERVER ERROR)	The generic answer for an unexpected failure if there is no more specific information available.