Debug School

rakesh kumar
rakesh kumar

Posted on • Updated on

Django REST API

django-rest-api

What is an API?
API is short for Application Programming Interface and it allows you to interface with other applications and pull/process/push data and values when required.

To simplify, in an API, we take in some data as a request from the client, process it, and then send something back to them.

Eg: A Facebook search, here the client, i.e., the user sends a profile request, the server then browses its database for the profile and returns them with the list of profiles( with that name).

CRUD operators and HTTP methods
While using an API, the can send requests to the server in many different ways according to its needs. These different types are called CRUD(create-retrieve-update-delete)

Image description

Image description

What is the REST API?

A REST (REpresentational State Transfer) API is similar to the standard API. We send the server a request. But the server doesn’t respond with data, it responds with resources.

Image description

Hence this can be thought of as an Item resource.

Therefore, now we can think of our interaction with the server as not with individual endpoint requests but with resources(having the same endpoint for different functionalities)

Also, another feature of Rest API is that it is stateless. I will explain this with an example.

If we post data about a new item chair, the server will add this information to the DB. Once added, the server forgets about it.

Now, if I hit the server with a GET request for that chair, the server will not remember that we just saved it in the previous interaction. It will again go back to the DB and search for an item named chair and then return the information.

After returning the information, it will again forget about it

Use of JSON in Client-Server API Interaction

APIs use JSON text for accepting and returning requests. That means, when you search Twitter for a particular tweet, you send the request to the server as a JSON. After processing it, the server sends back the Response again as a JSON text.

This JSON response is converted to readable format for the user.

The real exchange of information between the FRONT-END and BACK-END server in API happens using JSON text.

JSON text looks just like the python dictionary.

{“item”:
    { “chair”: {
        “Price”:120,
        “Color”: 'red',
        },
    }
}
Enter fullscreen mode Exit fullscreen mode

Installing DRF-Django Rest API Framework

Now to use Django REST API, we have an entire framework called the Django Rest framework.

We need to install that into our environment with the use of the pip command, just like how we installed Django.

So in your shell type:

pip install djangorestframework
Enter fullscreen mode Exit fullscreen mode

Image description

Installing POSTMAN

POSTMAN is an easy to use platform API development. Postman’s features simplify each step of building an API and streamline collaboration so you can create better APIs—faster.

To download postman,

  1. Go to the browser and search POSTMAN download.
  2. Click the first link
  3. Download the Free version of POSTMAN
  4. Follow the steps to install it

Top comments (0)