Getting started with Restful services using Django
Before we begin , if you do not have any idea about API and REST then read the paragraph about them else feel free to jump to next section.
What is an API?
API is the acronym for Application Programming Interface, which is a software intermediary that allows two applications to talk to each other. Each time you use an app like Facebook, send an instant message, or check the weather on your phone, you’re using an API. To know more , watch the video below 👇 .
What is REST?
REST is one of the most popular types of API or, as they’re sometimes known, RESTful APIs. REST or RESTful APIs were designed to take advantage of existing protocols. While REST — or Representational State Transfer — can be used over nearly any protocol, when used for web APIs it typically takes advantage of HTTP. This means that developers have no need to install additional software or libraries when creating a REST API.
Now that you have a basic knowledge of API and REST lets begin!
To learn about Django Rest Framework(DRF) the best way is to go through their official docs, the reason is that Django has the best documentations as compared to any other sources.
If you have some idea about how the normal view based django works it will be pretty much easier but that doesn’t mean that if you do not know much about the view based django you won’t be able to do DRF (I am an example for that 😉) . In this i will be covering the important points which will be used/needed while implementing endpoints. After going through the documentation you will get some rough idea about how everything works. I will not be covering how to setup each and everything, thats where documentation comes handy. I will discuss only about the points where things might get confusing.
As an example lets take a model for comment system with 3 fields email,content and created.
Serializers
One of the backbone of DRF is serializers. As in the example model taken above , When a comment object is to be sent to the front-end , the data is in complex-data form (querysets or model instances), this kind of data cannot be rendered in the front-end . So this data should be converted into a format which can be easily rendered into JSON,XML or other content types which is done by serializers. Serializers also provide deserialization,allowing parsed data to be converted back into complex types. So basically whatever we sent or receive from the front-end should go through these serializers.
Authentication
An important part of any project will be authentication!. Normally in django view based applications the process is just a simple form fetching and checking the username and password. While working with DRF Basically what happens is once you login with the credentials DRF will check the credentials and return a token which will identify the user who is currently logged in. This token can be saved in the local storage for further use.To access the user details of the currently logged in user You just have to pass the authentication token along with the header of all the request that you make. the user object of the currently logged in user can be obtained from request.user
.
Implementation of Authentication can be read from the documentation.For implementing social authentication in DRF there is a package from RealmTeam.
File Uploads
Another important part of DRF is parsers which can be explained by the file upload feature. Normally to send a data from front-end to back-end what we do is convert the required data into JSON and pass this data along with the request , but file objects cannot be converted into JSON. So instead of passing the data as JSON , it can be passed in as FORM-DATA but if you try that it will lead you to the error-The submitted data was not a file. Check the encoding type on the form.
This error comes up because of parsers. By default DRF sets the parser class as JSON parser which accepts only the data in JSON format , so when you sent the data as FORM-DATA it will not be parsed in the back-end. To resolve this the parser class has to be set to MultiPartParser. There are much more parsers which can be used depending on the requirements.
Testing Endpoints
After creating few simple API’s , We have to test these endpoints. The easiest method is by entering the URL in the browser and checking the output. For simple GET requests it will work but for other requests like PUT and POST where authentication is required it won’t . POSTMAN is one of the best application to test API’s by mimicking a HTTP request and the response which is received.. POSTMAN has a playground to test various requests which will help you get familiar with POSTMAN.
API Documentation
After creating all the required endpoints the main task is for the front end developers to integrate it to the application. So it will be a hard time for them finding out the URL’s they need! There again comes documentation to the rescue 😎 ! API docs which can be implemented as in figure. This will create an endpoint which will show us all the endpoints which are available. this feature almost reduces all the burden of documenting the endpoints and thus saving time of the front-end developers 😇.
Whatever i have explained in this blog is only a small part which will help you give a kickstart and go forward with DRF . You can go through the documentation of Django Rest Framework if you feel like knowing more. It is good to implement your application using DRF rather than view based because DRF allows us to use any front-end framework of our choice and the API’s can be integrated to it easily. At the end of the day it is your choice to choose which one 😉
Thank you for your valuable time and Have a great time hacking !😉