Learn API basics

Tram Ho


Here are some of the basic knowledge that I have learned on the way to find out what the API is.

We hope to receive your comments!

What is API

Technically, API stands for Application Programming Interface.

An API is a software intermediary that allows two applications to communicate with each other, and can be used for web-based system, operating system, database system, computer hardware, or software library.

In its simplest form, an API is an interface that allows one application to communicate with another through simple commands, and the way these commands are sent, and the format in which data is retrieved through the API may differ from SOAP or REST API.

What is RESTful API?

REST: is a type of data structure transformation, an architectural style for writing APIs. It uses a simple HTTP method to facilitate communication between machines. So, instead of using a URL for processing some user information, REST sends an HTTP request like GET, POST, DELETE … to a URL to process the data.

REST operates mainly on CRUD (Create, Read, Update, Delete) method, which is equivalent to 4 HTTP protocols: POST, GET, PUT, DELETE.

RESTful API: is a standard used in designing API for Web applications (such as designing Web services), to facilitate the management of resources. It focuses on system resources (such as text files, images, audio, video, or dynamic data …), including resource states that are formatted and transmitted over HTTP.

Many of you new to RESTful also often feel confused about how different REST and RESTful are. REST stands for Representational State Transfer, and applications that use REST design styles are called RESTful (-ful is the suffix like beauty and beautiful). Of course you can also use the term REST instead of RESTful and vice versa.

How does the API work?

The API is built on two main components: Request and Reponse

About the request

A standard request should have 4 things:

  1. URL
    The URL is the unique address for a request, usually the path to a logic handler function.
  2. Method
    HTTP requests have all 9 types of methods, the two most commonly used are GET and POST
  • GET: Used to get information from the server according to the given URI.
  • HEAD: Similar to GET, but the response has no body, only header.
  • POST: Sends information to the server via HTTP parameters.
  • PUT: Overwrites all information of the object with what is posted.
  • PATCH: Overwriting the object’s changed information.
  • DELETE: Delete the resource on the server.
  • CONNECT: Establishes a connection to the server according to the URI.
  • OPTIONS: Describes the communication options for the resource.
  • TRACE: Performs a loop-back test following the path to the resource.
  1. Headers
    It is the place that contains the necessary information of a request but end-users do not know its existence. For example: the length of the request body, the time it took to send the request, the type of device being used, the type of response format that the client can read …
  2. Body
    Is the place that contains the information that the client will fill out.

About the response

After receiving the request from the client side, the server will process that request and send back to the client a response. The structure of a response is quite similar to the request part, but the Status code will replace the URL and Method. In short, it has a 3-part structure:

  • Status code
  • Headers
  • Body
    The Header and the body are similar to the request.

The response’s status code

The status code is a 3-character integer, where the first character of the Status-Code defines the Response type and the last two characters do not have any sorting role. Come on. There are 5 values ​​of the first character:

  • 1xx: Information: When receiving such codes, the request has been received by the server and the request processing is continuing.
  • 2xx: Success: When receiving such codes, the request has been received, understood and processed successfully by the server.
  • 3xx: Redirection: This status code indicates that the client needs additional action to complete the request
  • 4xx: Client Error: It means the request contains incorrect syntax or was not executed.
  • 5xx: Server Error: It means that the Server failed to execute a seemingly possible request.

An example of request – response in the API

When you are using an application on a mobile phone, the application connects to the Internet and sends the data to the server. The server then retrieves that data, interprets it, takes the necessary actions, and sends it back to your phone. The app then interprets that data and presents you with the information you want in a readable way. This is what an API is – all of this happens through the API.

Before we get to the concept of expertise let’s take a familiar example. Imagine sitting in a restaurant and preparing to order. The chef – the “system” will cook the food for you. What is missing is the link between you and the chef. You do not have the ability to know which part of the kitchen in the restaurant to rush in and order food, that’s when you need a waiter – API.

This waiter will be the waiter, (or as we often call it the request – request) of you tell the chef what to do. The chef – the “system” knows what to cook for you and gives it to the waiter after it’s done. Then this waiter will bring what you need – food / information (or we call it response).

The article has been referenced


Share the news now

Source : Viblo