HTTP Basic auth web request in django

Viewed 807

I am new to programming.. I used python to create code that gets data from an API using requests package and the request uses HTTPBasicAuth. Below is the request which works fine in genral python.

response = requests.get(url,auth=HTTPBasicAuth('username','password'),stream=True

Can some please advise how can I implement this in django. when I use the same code as below, it gives me an error as "name HTTPBasicAuth is not defined"

def index(request):
   if request.method == "POST":
      url = urlname
      response = requests.get(url,auth=HTTPBasicAuth('username','password'),stream=True
      data = response.json()
2 Answers

Change your import statement to:

from requests.auth import HTTPBasicAuth 

you can also use: HTTPDigestAuth

import requests
from requests.auth import HTTPDigestAuth
Related