
RESTful web services have been used frequently in software applications recently. Developers often distinguish between client and server applications in software architectures. They also develop REST APIs in server applications catering to client requests. In this way, developers develop applications that are isolated from each other and suitable for layered system architecture. Have you ever wondered how REST API works, which is used frequently today?
Before asking how REST API works, it’s important to know exactly what a REST API means. Therefore, in this article, we will first introduce the concept of REST API. Then we will learn how the REST APIs work, find answers to many questions about the REST API and see many examples.
Table of Contents
What Is REST API?
REST is an architecture for client-server communication. In addition to developing in parallel with the HTTP protocol, the World Wide Web system actively utilizes it. The services that adopt REST architecture are commonly referred to as RESTful services. The main idea is to exchange data between client and server over HTTP protocol instead of using complex architectures such as SOAP (Simple Object Access Protocol) or RPC. Additionally, they offer developers a lightweight solution over other alternatives.
RESTful services run stateless and do not keep state in any request. Besides being simple, they are also quite flexible and capable.
How Does REST API Work?
In this section, we’ll take a step-by-step look at how a REST API works. REST is a server-client architecture, and its working principle is as follows.
Client Request: The client makes a request to obtain data from or modify the resource using the HTTP protocol. This request contains the HTTP method (GET, POST, PUT, DELETE, etc.), URI (Uniform Resource Identifier), and required data.
Server Processing: The server receives the request from the client and determines the destination to which it should direct the request. It makes processing decisions based on the HTTP method, URI, and other headers on the request. Then the server processes the request and makes the changes according to the flow of the application.
Creating a Response: The server generates an appropriate response to the request from the client. The response includes the HTTP status code, headers, and often a response message.
Client Response: The server sends the generated response back to the client.
What Are the Response Formats of the REST APIs?
One of the biggest advantages of REST APIs is the variety of response formats they provide. With the REST API, developers can provide returns in formats such as JSON, XML, CSV, HTML, Plain Text, Binary Data(JPEG, PNG, DOCX, MP3, and more), etc. In this section, we will examine JSON, XML, and CSV formats, which are the most commonly used response types in projects.
JSON
JSON stands for JavaScript Object Notation, and JSON is a minimal and readable format used to structure data. It holds data with key-value relationships. It is a very light and flexible data exchange format, and its main purpose is to exchange smaller sizes of data when exchanging data.
XML
XML is a text markup language, and it is similar to programming languages but has only certain functions. The primary purpose of documents prepared with XML language, which both humans and operating systems can read, is to store and transfer data. Therefore, almost all devices and operating systems can read it.
CSV
CSV files are plain text files. Files with this extension are text files on computers that contain letters and numbers. These are files that allow use in tables for data entry, compilation, and processing purposes.
What Are the REST API Request Methods?
REST APIs support many HTTP request methods. The most popular of these are HTTP GET, POST, PUT, and DELETE. In this section, we will see the use of these methods with examples. We will perform these examples via the JSONPlaceholder [1] and Postman [2].
HTTP GET Request
HTTP GET method is an HTTP method that the client application uses to obtain data from the server. To test this method, we will send an HTTP GET request with Postman to the ‘https://jsonplaceholder.typicode.com/posts‘ endpoint.
After performing this operation, the response we get is as follows:
HTTP POST Request
The HTTP POST method allows developers to create a new record in the server application. To test this, we will send an HTTP POST request with the following request body to the ‘https://jsonplaceholder.typicode.com/posts‘ endpoint.
1 2 3 4 5 |
{ "title": "foo", "body": "bar", "userId": 1 } |
After performing this operation, the response we get is as follows:
HTTP PUT Request
Developers prefer to use the HTTP PUT method when updating a record from their client application. In a common usage scenario, they include the ID of the record to be updated in the URL and add the updated data to the request body. The endpoint to which we will send the request is ‘https://jsonplaceholder.typicode.com/posts/1‘. The request body we will add for example usage is as follows:
1 2 3 4 5 6 |
{ "id": "1", "title": "foo_new", "body": "bar_new", "userId": 2 } |
After performing this operation, the response we get is as follows:
HTTP DELETE Request
Finally, we will examine the HTTP DELETE method. Developers use this method to delete a record from the client application. The endpoint to which we will send the request is ‘https://jsonplaceholder.typicode.com/posts/1‘.
After performing this operation, the response we get is as follows:
Conclusion
To sum up, the REST API is one of the most popular communication methods established between client and server. It offers developers speed, flexibility, isolation, and a fast development experience. In addition, they can be easily integrated into almost any project, providing data in formats such as JSON, XML, and CSV.
Sign up and discover free API products in many categories, and start to use them quickly.
FAQs
Q: What Is Uniform Interface in REST?
A: Uniform Interface in REST (Representational State Transfer) is one of the principles to have common, uniform interfaces between clients and servers. This not only simplifies the communication method but also allows each part to evolve independently of each other, thanks to a common interface. It is an important principle for client-server interactions.
Q: What Are the REST API HTTP Status Codes?
Some common HTTP status codes used in the REST API (Application Programming Interface) are as follows:
- 200 OK: The client’s request is successful.
- 204 No Content: The REST API request is successful, but the response does not contain any content.
- 400 Bad Request: The server could not understand the request or received it in an incorrect format.
- 401 Unauthorized: Request requires authentication or the provided credentials are not valid.
- 403 Forbidden: The server denies the request and does not authorize it.
- 500 Internal Server Error: The server encounters an error and cannot process the request.
Q: What HTTP Methods Does the REST API Support?
A: REST is an HTTP-based architecture. HTTP methods used by REST are as follows:
- GET
- POST
- PUT
- HEAD
- DELETE
- CONNECT
- OPTIONS
- TRACE
- PATCH
Q: What Is the Difference Between HTTP POST and HTTP GET?
A: When using the HTTP GET method, clients send requests in the URL section. Since this method displays the information sent in the URL, it poses a high-security risk. On the other hand, users utilize the HTTP POST method for requests that they do not want to display in the URL. It is more reliable but works slower than HTTP GET.
Q: What Are the Best Practices of REST API Development?
A: REST API development style varies from developer to developer. There is no rule for this, but there is a REST API design guideline accepted by many developers. According to this guideline, some considerations when developing a REST API are as follows:
- Resource Modeling
- Use Proper HTTP Methods
- API Versioning
- Use Proper Status Codes
- Request and Response Formats
- Authentication and Authorization
- Error Handling
[1] https://jsonplaceholder.typicode.com/
[2] https://www.postman.com/