Welcome, Guest: Register On Nairaland / LOGIN! / Trending / Recent / New
Stats: 3,151,646 members, 7,813,167 topics. Date: Tuesday, 30 April 2024 at 08:08 AM

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

Nairaland Forum / Science/Technology / Programming / I Need Help Integrating DHL API In Django (377 Views)

Help: Integrating Remita Payment Gateway For Single Payment/splitting Payment / I Need Help Integrating Remita / I Need Help Integrating Paystack (2) (3) (4)

(1) (Reply) (Go Down)

I Need Help Integrating DHL API In Django by Ononugo(m): 3:14pm On Sep 15, 2023
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)

Can You Edit Someone Else's Android App And Make It Suit You? / Which Is The Best ISP In Abeokuta / This Is The Reason You’re Not Making Money In Tech

(Go Up)

Sections: politics (1) business autos (1) jobs (1) career education (1) romance computers phones travel sports fashion health
religion celebs tv-movies music-radio literature webmasters programming techmarket

Links: (1) (2) (3) (4) (5) (6) (7) (8) (9) (10)

Nairaland - Copyright © 2005 - 2024 Oluwaseun Osewa. All rights reserved. See How To Advertise. 13
Disclaimer: Every Nairaland member is solely responsible for anything that he/she posts or uploads on Nairaland.