26 Nisan 2020 Pazar

WHAT IS REST?

                                   What is REST?

REST stands for Representational State Transfer.
It is an architectural style that facilitates systems to communicate with each other by providing certain standards on the web. It can also be said that REST is a communication approach that is frequently used in various web developments as well as architectural style.  

Let's say you want to go to the required cinema. You enter the website of a cinema company that has branches in different places over the Internet. There is always a lot of data that is changing, such as movies on display, different cities, movie session hours. Of course, we receive this data as a request to the server and a response to the client. The client requests the information from the server through an API, and then the server sends a response to the client.  Here, the response sent to the client is in the form of an HTML Web Page. However, we prefer that this data be returned in JSON, XML, etc. structured formats rather than complex HTML returns. Both JSON and XML formats have an appropriate hierarchical structure of the data. However, the only problem available so far in this framework is that you need to use many methods to obtain the necessary information. Using these methods to get information becomes quite complex when you need complex data. It is much faster to get data or manipulate it in various formats (XML, JSON, HTML, TEXT etc.).  This is where the REST API comes into play. The REST API creates an object and then sends the values of an object in response to the client. Here you have an object and you submit the status of an object. That is why it is known as the REST Representative State Transfer. 
The REST architectural structure has 6 limitations:
  1. Client-server architecture
  2. Statelessness
  3. Cacheability
  4. Layered system
  5. Code on demand (optional)
  6. Uniform interface

If an API has all the limitations of the REST architecture, then it is called the RESTful API.

NOTE: REST API Request can be used by any site or application, regardless of what language it is written because it is based on the universal HTTP protocol and is usually returned in JSON format, where almost all programming languages can be read.

In the REST architectural style, the implementation of the client and the implementation of the server can be done independently, each unaware of the other. This means that the code on the client-side can be changed at any time without affecting the operation of the server, and vice versa. As long as each party knows which format to send messages to the other, they can be kept modular and separate. Also, this separation process allows each component to develop independently. Using a REST interface, different clients can hit the same REST endpoints, perform the same actions, and receive the same responses. As you understand, the systems that follow the REST paradigm are stateless, and also, the server does not need to know anything about the client's state. In this way, both the server and the client can understand received messages without seeing previous messages. This stateless constraint is implemented using resources rather than commands. In REST, server-side information is considered a resource where developers can access web URIs uniformly using HTTP.

REST APIs allow developers to create any type of web application to perform CRUD operations (Create, Read, Update, Delete). According to the REST directives, it is necessary to use Web HTTP methods to call the server. Here are the four most important HTTP methods to obtain the necessary resources from the server and their equivalent CRUD operations : 


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


The Anatomy of A Request

  1. The Endpoint 
  2. The Method
  3. The Headers
  4. The Data (or body)


Endpoint
Is the URL you requested. 
Is the starting point of the API you requested.

In this article, we will use the command-line utility called cURL. We will use cURL because API documents are normally written concerning cURL. If you understand how to use CURL, you won't have trouble understanding API documentation. Then, you can easily fulfill your requests with your preferred language. 

Check your curl version first:
curl --version

If cURL is not installed, you will receive a "command not found" error.  Install cURL :
 sudo apt install curl

When using cURL, you must type cURL followed by the desired endpoint. For example, let's do this for Github:
curl https://api.github.com

you will get such an output :




The Method

The method is the type of request you send to the server. You can choose from these five types below:
  • GET
  • POST
  • PUT
  • DELETE
  • PATCH
These methods provide meaning for the request you’re making. They are used to perform four possible actions: Create, Read, Update and Delete (CRUD). 
You have seen their correspondence in the table above.









You can set the request method in cURL by writing -X or --request, followed by the request method. This command below tries to create a repository via cURL:

curl -X POST https://api.github.com/user/repos
you will, of course, have to verify identity when you try to run this request.



The Headers

Headers are used to provide information to both the client and server. It can be used for many purposes, such as authentication and providing information about the body content.
HTTP Headers are property-value pairs that are separated by a colon. The example below shows a header that tells the server to expect JSON content :

"Content-Type: application/json". Missing the opening ".

You can send HTTP headers with the curl with the option -H or --header. For example, let's send the header above to Github's API:

curl -H "Content-Type: application/json" https://api.github.com

You can use the -v or --verbose option to send the request, for example:

curl -H "Content-Type: application/json" https://api.github.com -v




The Data (or Body)

The data (sometimes called “body” or “message”) contains information you want to be sent to the server. This option is used only in POST, PUT, PATCH or DELETE requests.

To send data through cURL, you can use the -d or --data option:
curl -X POST <URL> -d property1=value1









https://www.smashingmagazine.com/2018/01/understanding-using-rest-api/

Hiç yorum yok:

Yorum Gönder