postman’s GET request is running, but it does not work with python (HTTPSConnectionPool)
I’m trying to get JSON from an API that can then be displayed on a website built with django, which is made using rest_framework. I’ve tried Requests and http.client but I’m getting the same error :
HTTPSConnectionPool(host=’webpageUrl’ port=443): The maximum number of retries exceeds url:/api/donations (indicated by ConnectTimeoutError(
I’ve tried debugging with a GET request in Postman and it works great, but I get the above error when I use the example provided in the Requests library documentation or use the code snippet generated in Postman. Also, I used to have basic authentication and I thought this was the source of the problem, so I turned off authentication but it didn’t work.
This is the code I’m currently trying :
import requests
def available_donations(request):
assert isinstance(request,HttpRequest)
response = requests.get("webpageUrl/api/donations", headers={'Content-Type':'application/json'}, timeout=5)
return render(
request,
'app/availableDonations.html', {'donation':response},
{
'title':'Overview of available donations'
})
I
expected to get an error about web page rendering, but from what I’ve read from different stackoverflow issues, the error means it can’t find the page.
Thanks in advance!
Edit 1:
I tried running the snippet code from postman on my personal computer and it worked fine. I think the problem is that it can’t make HTTP requests to the API (on the same server) using the method I use. Also, I tried to run a virtual different virtual API using this efficient method.
Solution
I
found that the method I used didn’t work for internal HTTPS requests. I use ifconfig to look at the local IP address and use it instead of the host and put the IP address in the ALLOWED_HOSTS of settings.py. Also, I disabled SSL validation with verify=False
def getDonations():
url = "https://172.19.10.5/api/donations"
payload = ""
headers = {
'cache-control': "no-cache",
}
response = requests.request("GET", url, data=payload, headers=headers, timeout=5, verify=False)
print(response.text)
return response.text