RESTful API

Introduction to RESTful Services.

What is REST ?

REST is an acronym for REpresentational State Transfer.

It is a software architectural style for providing standards between computer systems on the web which making it easier to communicate with eachother.

RESTful Services.

REST-compliant systems that conform to REST architectural style are often called as RESTful services.

They are characterized by how they are stateless, and separate the concerns of client and server.

Build around things or resorces – Resource based.

Resource are identified by a URI.

Multiple URIs may refer the same resource.

Consists Client and Server communication

JSON or XML is used to pass data.

Lightweight, scalable and maintainable.

Seperation of Client and Server.

  • Use URI to make the connection between CLient and Server.
  • Clearly separates user interface and services. Client is portable, it does not have any connection to data server. Server stand independent from different user interfaces.
  • HTTP stack is the communication platform.

In REST, the implementation of the client and the server is possible without each thinking about the other. This implies the code on the client side can be changed whenever without influencing the tasks of the server, and the code on the server side can be changed without influencing the client’s operations.

As long as each side comprehends what messages to send to the next, they can be kept measured and discrete. Moreover, the detachment permits each component to evolve independently.

Statelessness.

  • No client state on the Server
  • Self containing messages. ( each message contains sufficient information for that particular operation )
  • Session state will be kept in client side.

Systems that pursue the REST paragigm are stateless, implying that the server does not have to know anything about what state the customer is in and vice versa. both the server and the customer can see any message got, even without seeing past messages. This limitation of statelessness is implemented using resources, rather than commands. Resources are the nouns of the web. They describes any document, object or thing that you may need to store or send to different service.

Now, lets explore how the communication between the Client and the Server acctually happens when we are implementing a RESTful service.

Communication between Client and Server.

In REST Clients send requests to retrieve or modify resources and Servers send responces to the particular requests.

Making Requests.

Clients make requests to the Server in-order to retrieve or modify data. A simple request generally consists of:

  • an HTTP verb.
  • a header.
  • a path to a reasource.
  • an optional message body containing data.
HTTP request message

HTTP Verbs.

HTTP verbs define what kind of a operation is to be performed.

Headers.

The header of a request contains the type of the content that is able to recive from the Server. Therefore, this is called as the Accept field, which ensures that the server does not send data that processed by the client or cannot be understood.

Paths.

Requests must contain a path to a resource that specifying where the operation should be performed on.

Sending Responses.

As well as requests, Responces generally consists:

  • Response codes.
  • Content types

Response Codes.

Response codes are status codes which alert the Client about the success or about the faliure of the operation.

most common status codes.

  • 200 – OK
  • 201 – Created
  • 202 – Accepted
  • 204 – No content
  • 301 – Moved permenently
  • 302 – Found
  • 304 – Not modified
  • 400 – Bad request
  • 401 – Unauthorized
  • 403 – Forbidden
  • 404 – Not found
  • 405 – Method not allowed
  • 409 – Conflict
  • 412 – Precondition failed
  • 500 – Internal server error
  • 502 – Bad gateway
  • 503 – Service unavailable

Examples of Requests and Responses.

Think we have an application that allows you to view, create, edit and delete users hosted at example.com.

If we want to view all users, the request would be as follows:

GET http://example.com/users
Accept: application/json

The response header would be as:

Status Code: 200 (OK)
Content-type: application/json

If we want to create a new user by posting data:

POST http://example.com/users
Body:
{
  “customer”: {
    “name” = “roma”
    “email” = “roma.code.blog”
  }
}

The response header for this particular instance would be like:

201 (CREATED)
Content-type: application/json

If we want to view a single specific user we can GET it by specifying the particular user’s id:

GET http://example.com/users/1234567
Accept: application/json

If we want to update a particular user’s details:

PUT http://example.com/users/1234567
Body:
{
  “customer”: {
    “name” = “romesh”
    “email” = “romesh.code.blog”
  }
}

If we want to delete a user:

DELETE http://example.com/users/1234567

And thats it for the Introduction to RESTful Services. Hope you have got the basic idea of what RESTful services are, about its core features and how to get start with REST.

. . .

Thanks for reading! ❤️

If you have any queries regarding this article, feel free to comment out in the comment section below or contact me on twitter.

Leave a Reply