REST-API developed using Python & Django

Aditya Kumar
4 min readFeb 14, 2020

--

Intro to REST & API

REST and API are two terms commonly heard whenever referring to web services and technologies. They are often combined together and called REST API. However, It is a common misconception to believe that REST API is somewhat of a single entity or technology.

The Difference

REST is more like architecture or guiding principles for building an API. It can be alternatively thought of like a blueprint.

API, on the other hand, is an abbreviation for Application Programming Interface which as the name suggests allows different applications to communicate or interact with one another.

Therefore, REST provides you the blueprint for building an API which in turn makes use of the URLs and the HTTP protocol to facilitate interaction between devices and an API(Note, API is being talked as an interface here) acts as a single point of access to a service or resource.

You can read more about the above-stated guiding principles here: https://restfulapi.net/

On the Internet

The URL that you enter in the address bar is a “Request” and the data set back to you on the basis of the “Request” is called “Response”. These resources on the internet as you can guess is accessed using the URL.

Now, when it comes to requests and responses, they have their own formats and are important to understand when working around with HTTP requests or API in general. They are as follows:

Request

It consists of the endpoint, method, headers and the data.

  1. An endpoint is a URL that contains the data you request.
  2. A method is a type of request, there are basically 5 HTTP methods that are used by REST API
  • GET — used to obtain data
  • POST — used to create data
  • PUT — used to update data (used to create (if data doesn’t exist) or update the entire resource)
  • PATCH — used to update data (used to update the resource partially, but fails if a resource doesn’t exist already)
  • DELETE — used to delete data

3. Headers provide information about the body content or can also be used for authentication or caching purposes

4. Data is the information that you want to receive/retrieve or send to the server and is usually sent in a similar way to an HTML <form> submission.

Response

When it comes to responses, there is no common standard and different APIs follow different formats. For example, as per the Google JSON style guide, they have divided the responses into a success response that returns data and error response that returns error code along with a message. However, from a general point of view, responses do contain:

  1. Error_code which gives you code such as 403, 404, 405, etc on the basis of which you can further debug the request
  2. Success: is a Boolean value (True or False)
  3. Message: is sometimes returned and sometimes not, depending upon the implementation
  4. Data: contains the data you requested for

It is important to note that the REST doesn’t specify the return formats as in what the format should be of the data, although, prominently it is JSON because it is both in a human-readable format as well as the machine-readable format in addition to other advantages. It basically consists of key-value pairs thus it can be thought of like a dictionary, record or hash table, etc. For e.g.,

{“employee”: {“id”: 1,“name”: “Admin”,“location”: “USA”  }}

A Common Problem

REST API is limited by their implementation which is to say that if not implemented in a robust way, there are chances of the common N+1 problem occurring according to which for every request you make for a child object, a corresponding request will be made for the parent object as well.

Example

You want to get the top 10 posts on Facebook on the basis of likes. Then, in order to obtain the posts, the client will request 10 /posts/ details ordered by the number of likes. The response from the server will contain a list of posts with post id. However, to obtain the posts, you also need to get the profiles to whom the posts belong to therefore in addition to requesting for /posts/, you’re also basically requesting for /profiles/.

Thus, the N+1 problem. In order to obtain the post which can be considered as a child object here, you also need to obtain the profile to which it belongs, in which case it becomes the parent object here.

Solution

Facebook, themselves have created the solution for the above problem in the form of GraphQL which is a web service query language. It can be thought of as SQL for web services where a single request describes the data you want and how you want it.

That was all for the REST & API and its constituents. I definitely recommend reading up more on it for e.g. other common problems, best practices of implementing REST API, etc.

Project

You can further check my project on REST API where I implemented features like Token Authentication, CRUD operations on profiles and feed items (similar to the Facebook wall, only in functionality though not appearance) with clear instructions on how to execute and details of everything in it.

Thank you!

--

--