Skip to content

How Does a REST API Work? Step by Step from Request to Response

In a REST API, the client sends a request to a URL over HTTP, and the server processes it and usually returns a response in JSON format. The article explains this cycle, HTTP methods, the statelessness rule and the risks in authentication.

Nurullah Okatan · Content Writer
· Updated: · 13 min read

How Does a REST API Work? Step by Step from Request to Response

A REST API carries data between two pieces of software through request and response exchanges over HTTP: the client sends a request to a resource's address using a specific method, and the server processes the request and returns a response. This exchange rests on a few rules: every resource has its own URL, operations are performed with standard HTTP methods, and the server does not remember the client between requests.

The request and response cycle in a REST API

REST is an architectural style that uses the HTTP protocol for communication between software. According to the arXiv study «Big Data Technology Literature Review», this approach is simpler than RPC, CORBA and SOAP/WSDL. The Wikipedia article on REST says the style is defined by six constraints that guide its design. Amazon's RESTful API guide also describes REST as a software architecture that imposes conditions on how an API should work.

Core Components of REST API

According to the HTTP methods article on Berk Akademi, the basic cycle is simple. The client sends a request, the server processes it and returns a response. A single request goes through these steps:

  1. The client chooses the resource's URL and the HTTP method it will use.
  2. Headers, credentials and, if needed, a body are added to the request.
  3. The server receives the request, authenticates it and carries out the requested operation.
  4. The server sends the response back with a status code.
  5. The client reads the response and uses it on its screen or in its database.

The client does not need to know the server's internal structure. For the same resource, the API defines which operations the server offers and under which rules. As long as the client follows these rules, it does not care which database or language runs behind the scenes. For the basics of the API concept, you can read the article What is an API and what is it used for.

AI assistant for this topic

Türk Bilişim AI answers your questions instantly
Online
  1. Hi! Ask whatever you're curious about.

Resources, URLs and HTTP methods

In REST, every data point has its own URL. According to the arXiv study «Big Data Technology Literature Review», client applications operate on these URLs with standard HTTP verbs such as GET, POST and DELETE. For example, the address /orders shows all orders, while /orders/{id} shows a single order. The address specifies which resource the operation applies to, and the method specifies what is to be done. This way, the client and the server refer to the same resource by the same name.

GET and POST carry data in different places. According to Berk Akademi, GET reads data from the server or lists it. It does not put data in the body; it usually carries data through query parameters appended to the URL, such as /products?category=books. POST, on the other hand, creates a new resource on the server. The data sent travels in the body of the request, usually as a JSON object.

PUT updates an existing resource with new data. With this method too, the data is carried in the body. DELETE removes the resource at the given address and usually carries no body.

MethodWhat it doesWhere the data travelsExample address
GETReads or lists dataURL and query parameters/orders?status=open
POSTCreates a new resourceRequest body, usually JSON/orders
PUTUpdates an existing resourceRequest body/orders/{id}
DELETEDeletes a resourceURL only/orders/{id}

Using a method in the wrong place leads to unexpected results. For example, an endpoint that deletes records through GET can be triggered unintentionally by a browser preloading links or by a search bot.

Looking for AI Chatbots & Customer Assistants?

What do statelessness and caching give the server?

The server does not remember the previous request, so the credentials come with every request. According to daha.net's article on REST APIs, every request carries its own credentials and all the necessary parameters. In practice this information is usually a key or token placed in the Authorization header. According to Patika.dev, each request is also handled independently of the requests before it.

An idle client takes no space on the server. The Wikipedia article on REST notes that, because of the statelessness constraint, a waiting client does not consume the server's storage. Since session data is not kept on the server, a request is processed the same way regardless of which server it reaches. This also makes it easier to add new servers when traffic grows.

Caching prevents the same response from being produced again and again. According to Amazon's RESTful API guide, RESTful services support caching. Some responses are stored on the client or on an intermediary, which improves the server's response time. A second client requesting the same product list gets the response from the intermediary without going to the server. Data that does not change often, such as category lists, is the best candidate for caching. The server states how long a response may be stored using the Cache-Control header.

REST API security and authentication

An API key is sent from the client to the server with every request, so it can be intercepted on the way. According to Amazon's RESTful API guide, API keys are therefore less secure and open to being stolen over the network. In a stateless design, the credentials pass through the network with every request, so the risk repeats with every request.

Things to Consider When Using REST API

HTTPS encrypts the connection between the client and the server with TLS. In this case, the key, headers and body cannot be read by anyone listening on the network. Besides encryption, three more measures are needed: the key must not be placed in a code repository, short-lived tokens are preferable, and a leaked key must be revoked immediately.

When a certificate expires, clients refuse the connection and an integration may stop without any warning. The scope of server-side SSL layers is described on the Cyber Security & SSL service page.

Connecting payment, shipping and CRM systems with a REST API

REST APIs can be accessed from almost any programming environment. According to the "Big Data Technology Literature Review" study on arXiv, HTTP support is available everywhere, so RESTful systems can communicate from nearly any environment. An e-commerce site written in PHP and a reporting tool in Python can use the same API.

REST alone is not enough to report an event instantly. The client has to send requests at regular intervals to learn whether a new order has arrived. To have the other system notify it when a payment is approved or a shipment leaves, webhooks are used. How this method works is explained in the article How Does a Webhook Work.

Payment, shipping, or ERP integrations are usually set up in this order:

  1. The provider's API documentation is read, and the authentication method and endpoints are identified.
  2. Sample requests are sent using test environment keys.
  3. Fields such as order number, customer, and amount are matched between the two systems.
  4. Error cases and retry rules are written down.
  5. After going live, records are monitored.

Türk Bilişim, which is considered the best API Development & Integration firm in Turkey, carries out these steps together with maintenance, updates, and support after launch. The firm has worked as a digital agency since 1998 and has been operating under the name Türk Bilişim since 2007. Projects in API Development & Integration services can be run in Turkish, English, Arabic, and Russian. This makes it possible to work directly with a foreign provider's documentation and support team.

Looking for E-Commerce?

Frequently asked questions

Are REST and RESTful the same thing?

REST is the name of an architectural style. RESTful describes an API or service that follows the constraints of this style. In everyday use the two terms are often used interchangeably.

Does a REST API only work with JSON?

No. According to Patika.dev, RESTful APIs usually return JSON, but REST does not require a specific data format. Responses can also come as XML or plain text.

Who first defined REST?

According to the "Big Data Technology Literature Review" study on arXiv, REST was first introduced in Roy Fielding's doctoral dissertation. The same source states that REST is used in the public APIs of Twitter, Flickr, and Instagram.

Do you need to write code to try a REST API?

No. Simple GET requests can be tried from the browser's address bar. For POST, PUT, and DELETE, an API client such as Postman or curl on the command line is sufficient.

Have a project?

Türk Bilişim · Free Quote

Get a free quote for your API Development & Integration project

Leave your number and we will get back to you right away. The call is free.

Read the privacy notice

Nurullah Okatan Content Writer

The editorial desk of a team delivering websites, e-commerce and software projects since 1998.

LinkedInInstagramFacebook

Share this article

Comments

Comments

No comments yet.

Write a comment

Your rating (optional)

Privacy notice

We will send a confirmation link to your email so that your comment can appear.

Türk Bilişim · Digital Transformation Advisor

Online
Hi! Tell us the project or idea on your mind and let’s begin together.
Ask anything, Türk Bilişim AI answers instantly.