Skip to content

Basics of REST APIs

Alexia edited this page Mar 2, 2020 · 1 revision

REST stands for REpresentational State Transfer and is used to access and manipulate data using several stateless operations. These operations are integral to the HTTP protocol and represent an essential CRUD functionality (Create, Read, Update, Delete).

Some basic constraints for situations where you want to use REST:

  1. Client-Server structure. One system makes an HTTP request to a URL hosted by another system, which returns a response. It's like how a browser works, where a specific URL routes to a specific web service with an HTML page at that address.

  2. Stateless. REST is stateless: the client request should contain all the information necessary to respond to a request. In other words, it should be possible to make two or more HTTP requests in any order and the same responses will be received.

  3. Layered. The requesting client need not know whether it’s communicating with the actual server, a proxy, or any other intermediary.

RESTful web service requests will contain:

  1. An Endpoint URL. An application implementing a RESTful API will define one or more URL endpoints with a domain, port, path, and/or query string — for example, https://mydomain/user/123?format=json.

  2. The HTTP method.

The HTTP operations available are:

  • POST (create a resource or generally provide data)
  • GET (retrieve an index of resources or an individual resource)
  • PUT/PATCH (create or replace a resource)
  • DELETE (remove a resource)

Examples:

  • a GET request to /user/ returns a list of registered users on a system
  • a POST request to /user/123 creates a user with the ID 123 using the body data
  • a PUT request to /user/123 updates user 123 with the body data
  • a GET request to /user/123 returns the details of user 123
  • a DELETE request to /user/123 deletes user 123
  1. HTTP headers. Information such as authentication tokens or cookies can be contained in the HTTP request header.

  2. Body Data. Data is normally transmitted in the HTTP body in an identical way to HTML

    submissions or by sending a single JSON-encoded data string.

Using the above-listed operations and a resource name as an address, we can build a REST API by basically creating an endpoint for each operation.

Express is a really Node framework that is designed to help JavaScript developers create servers quickly.

Tiny example:

app.get("/url", (req, res, next) => {

res.json(["Tony","Lisa","Michael","Ginger","Food"]);

});

This simple function makes the express app to use the url handle “/url” to trigger the callback that follows it. The callback accepts three parameters, req is the request body and carries information about the request. The res is the response body and is used to handle response functions like .render() to render templates and .json() to return json data. You might be wondering where the REST attribute comes in. REST stands for REpresentational State Transfer. This means there is no state between the client and the server. There are no webpages served to be parsed, just data. And this gives you all the freedom you need. All you need to do is write some logic on a specific URL that connects to a database, uses it’s logic to process the data and return it in JSON format.

This is the whole point of using REST, it makes the connection stateless therefore any client that utilizes the HTTP protocol can access this data. Now you can iterate through the data and display it anywhere you want.

I take no credit for the information or examples in this page. All information and examples came from these helpful resources, which I would recommend for further reading:

Clone this wiki locally