REST API

REST API

REST API is a software architectural style for Backend.

REST = “REpresentational State Transfer”.?API = Application Programming Interface

Its purpose is to induce performance, scalability, simplicity, modifiability, visibility, portability, and reliability.

REST API is?Resource-based, a resource is an object and can be access by a URI. An object is “displayed”/transferred via a?representation?(typically JSON). HTTP methods will be actions on a resource.

Example:

  • Resource:?Person?(John)
  • Service: contact information (GET)
  • Representation:
  • first_name,?last_name,?date_of_birth
  • JSON format

There are 6 constraints:

1. Uniform Interface

  • Define the interface between client-server
  • Simple and can be split in small parts

HTTP verbs

  • GET:
  • Read representation of a resource or a list of resources
  • POST:
  • Create a new resource
  • PUT:
  • Update an existing resource
  • DELETE:
  • Remove an existing resource

URIs - resource name

A resource representation is accessible by a URI:

  • GET /users: path for listing all user resources
  • GET /users/12: path for the user?id = 12
  • GET /users/12/addresses: path for listing all addresses of the user?id = 12
  • POST /users: path for creating a user resource
  • PUT /users/12: path for updating the user?id = 12
  • DELETE /users/12/addresses/2: path for deleting the address?id = 2?of the user?id = 12

HTTP Response

In the HTTP Response, the client should verify the information of two things:

  • status code: result of the action
  • body: JSON or XML representation of resources

Some important status code:

  • 200: OK
  • 201: created => after a?POST?request
  • 204: no content => can be return after a?DELETE?request
  • 400: bad request => the server doesn’t understand the request
  • 401: unauthorized => client user can’t be identified
  • 403: forbidden => client user is identified but not allowed to access a resource
  • 404: not found => resource doesn’t exist
  • 500: internal server error

2. Stateless

The server is independent of the client. The server doesn’t store user client information/state. Each request contains enough context to process it (HTTP Headers, etc.)

Some authentication systems like OAuth have to store information on the server side but they do it with REST API design.


3. Cacheable

All server responses (resource representation) are cacheable:

  • Explicit
  • Implicit
  • Negotiated

Caches are here to improve performances. In a REST API, clients don’t care about the caching strategy, if the resource representation comes from a cache or from a database…


4. Client-Server

REST API is designed to separate Client from the Server. The server doesn’t know who is talking to it. Clients are not concerned with data storage => the portability of client code is improved. Servers are not concerned with the user interface or user state so that servers can be simpler and more scalable

5. Layered System

Client can’t assume direct connection to server. Intermediary servers may improve system scalability by enabling load-balancing and by providing shared caches. Layers may also enforce security policies.

6. Code on Demand (optional)

Server can temporarily:

  • Transfer logic to client
  • Allow client to execute logic
  • Example: JavaScript

要查看或添加评论,请登录

Hassan Juma的更多文章

  • Type annotation for A strongly and dynamically typed Python.

    Type annotation for A strongly and dynamically typed Python.

    Python is a dynamically-typed language. That means that variable types are dynamically set at run-time, upon assignment…

  • What is a child process?

    What is a child process?

    Although it may sound like something out of a parenting handbook or a psychological journal, the term child process…

  • Data Profiling with its Benefits, Best Practices & Tools

    Data Profiling with its Benefits, Best Practices & Tools

    What is the importance of data to a business? Good data is the core of most effective business decisions and…

  • Containers and Containerization

    Containers and Containerization

    What containers really are and why do we need them? Containers are a solution to the problem of how to get the software…

  • Server Monitoring

    Server Monitoring

    What do we really mean by Server Monitoring? Cloud service providers like Amazon Web Service (AWS), Google Cloud and…

  • Monitoring

    Monitoring

    Just as the heart monitor in a hospital that is making sure that a patient’s heart is beating and at the right beat…

  • Web stack debugging

    Web stack debugging

    Intro Debugging usually takes a big chunk of a software engineer’s time. The art of debugging is tough and it takes…

社区洞察

其他会员也浏览了