Karthik Viz
Karthik Viz

Reputation: 125

HTTP Basic auth web request in django

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()

Upvotes: 3

Views: 1590

Answers (2)

Houda
Houda

Reputation: 691

you can also use: HTTPDigestAuth

import requests
from requests.auth import HTTPDigestAuth

Upvotes: 0

Jack
Jack

Reputation: 361

Change your import statement to:

from requests.auth import HTTPBasicAuth 

Upvotes: 2

Related Questions