yousof
yousof

Reputation: 287

How to use api in DRF with authentication in python

I want to make an API with token Authentication. Now i can use Postman But in other hand i don't know how can i used it in python.

I tried this into python:

import requests
response = requests.get("http://127.0.0.1:8000/api/v1/get-all-data/ 'Authorization: Token f468db88cb3b4e92ba00f662a4f334d47bd27f38'")


print(response.json())

but it gives me an error: json.decoder.JSONDecodeError: Expecting value: line 1 column 1 (char 0)

Upvotes: 0

Views: 62

Answers (1)

Brian Destura
Brian Destura

Reputation: 12068

Try with this:

response = requests.get(
    "http://127.0.0.1:8000/api/v1/get-all-data/",
    headers={'Authorization': 'Token f468db88cb3b4e92ba00f662a4f334d47bd27f38'}
)

The header should be set as a kwargs of headers. If it still fails, check if it gives you any information from response.text

Upvotes: 1

Related Questions