Creating Python APIs - The Django REST framework. Basic authentication[14/n]

Welcome back to my series on creating Python APIs using Django REST framework.

Today we will view basic authentication in our API.

So far we've been using the API after signing into the website but what if someone wants to access the API via a mobile device or using the curl command.

Let us try to use the curl command on the APIs current todos page (127.0.0.1:8000/api/todos).

image.png

The command to use is curl http://127.0.0.1:8000/api/todos/

You should see something like the below:

image.png

It basically says that we are not authenticated to be able to view todo objects, which is great security because we don't want other users to be able to view todo objects of other users.

You could use basic authentication passing the username and password as string with the url as follows:

curl http://127.0.0.1:8000/api/todos/ -u 'saurav:abc123'

However, if we try to access the todo objects we would have to pass it as json in our api which is a very tedious process.

In the next article we will talk about allowing the user to sign up via our API.

  • You can view the entire code on my github profile.

Hope you enjoyed this post! If you happen to like it, feel free to share. You can also follow me on Twitter on my coding journey.

 
Share this