₦airaland Forum

Welcome, Guest: RegisterLoginWith GoogleTrendingRecentNew

Stats: 3,324,981 members, 8,419,815 topics. Date: Wednesday, 03 June 2026 at 11:43 PM

Toggle theme

I Need Help Integrating DHL API In Django - Programming - Nairaland

Nairaland ForumScience/TechnologyProgrammingI Need Help Integrating DHL API In Django (727 Views)

1 Reply (Go Down)

I Need Help Integrating DHL API In Django by Ononugo(op):
I have been trying to integrate DHL API into my Django project but I keep getting error with 401 error code. I have created an app on the DHL developer portal and it has been approved so I have my API Key. I don't know what is wrong. Let me attach a simple code I tested with.

class DHLApiView(APIView):

def get(self, request):
import requests

url = "https://api-test.dhl.com/track/shipments"
headers = {
"DHL-API-Key": settings.DHL_API_Key,
}
params = {
"trackingNumber": "00340434292135100186",
"limit": 5,
}

response = requests.get(url, headers=headers, params=params)

if response.status_code == 200:
dhl_data = response.json()
print(dhl_data)
else:
print(f"Error: {response.status_code} - {response.text}"wink

return ApiResponse(200).response()

Re: I Need Help Integrating DHL API In Django by jesmond3945: 7:23pm On Sep 15, 2023
add your imports let me check. Then place the import requests outside the class
Re: I Need Help Integrating DHL API In Django by Karleb(m): 9:02pm On Sep 16, 2023
Does python allows import in a class method? 😳😱
Re: I Need Help Integrating DHL API In Django by Tunagy: 12:40pm On Sep 17, 2023
Karleb:
Does python allows import in a class method? 😳😱
yes, is it a bad practice sir, or does it has any use cases?
Re: I Need Help Integrating DHL API In Django by Karleb(m): 1:21pm On Sep 17, 2023
Tunagy:
yes, is it a bad practice sir, or does it has any use cases?
I honestly don't know. I'm just surprised it's possible. It looks like witchcraft to me. Lol
Re: I Need Help Integrating DHL API In Django by Xinjin: 3:34pm On Sep 17, 2023
The APIView is a cbv to handle incoming requests to your web app and responds appropriately. Why are you sending requests to a third party API inside of it?
Re: I Need Help Integrating DHL API In Django by Spicyneedle: 5:44pm On Sep 17, 2023
Ononugo:
I have been trying to integrate DHL API into my Django project but I keep getting error with 401 error code. I have created an app on the DHL developer portal and it has been approved so I have my API Key. I don't know what is wrong. Let me attach a simple code I tested with.

class DHLApiView(APIView):

def get(self, request):
import requests

url = "https://api-test.dhl.com/track/shipments"
headers = {
"DHL-API-Key": settings.DHL_API_Key,
}
params = {
"trackingNumber": "00340434292135100186",
"limit": 5,
}

response = requests.get(url, headers=headers, params=params)

if response.status_code == 200:
dhl_data = response.json()
print(dhl_data)
else:
print(f"Error: {response.status_code} - {response.text}"wink

return ApiResponse(200).response()
401 that's your request is not authorized.
Check whether if DHL-API-Key is not empty or maybe it might be the way you are trying to get response.
Try this response = requests.get(url, params=params, headers=headers)
Re: I Need Help Integrating DHL API In Django by bularuz(m): 4:01am On Sep 20, 2023
Tunagy:
yes, is it a bad practice sir, or does it has any use cases?
There are times it is necessary
1 Reply

Help Integrating LENCO Payment APIHelp: Integrating Remita Payment Gateway For Single Payment/splitting PaymentI Need Help Integrating Remita234

Experienced React Developer NeededPls See This Awsome AppPHP Progaammers, Drop Your Pins Here