Issue
I'm facing pretty much what the title says. I have an Ubuntu server running 20.04.5 LTS running a Django application. I'm using the requests
module to send API requests. However, whenever I try a request from the server - either from the application or a separate Python script or even cURL - I always get a 403 Forbidden response.
But if I run the same command from my local machine - cURL/Postman/script - I get a 200 Response. The requests
code is
import requests
headers = {
'Content-Type': 'application/json',
}
body = {
/*Stuff related to authentication*/
}
token_url = 'https://example.com/token'
print("Attempting to get access token using the REQUESTS http module:")
response = requests.post(token_url, json=body, headers=headers)
print("Request Data:\n")
print(f"Request Headers: {response.request.headers}")
print(f"Response HTTP Status: {response.status_code}\nResponse body is {response.text}")
Obviously, example.com
isn't really the endpoint I'm attempting to hit up.
The corresponding output from my server is
Attempting to get access token using the REQUESTS http module:
Request Data:
Request Headers: {'User-Agent': 'python-requests/2.22.0', 'Accept-Encoding': 'gzip, deflate', 'Accept': '*/*', 'Connection': 'keep-alive', 'Content-Type': 'application/json', 'Content-Length': '226'}
Response HTTP Status: 403
Response body is
Response content b''
I've been facing this issue since I updated my server about 2-3 days ago. requests
suddenly has stopped working and I've no idea why. I have no proxies installed on the server and I've confirmed with the API provider that they haven't blocked my server's domain.
I've changed HTTP modules and used urllib3
and even that doesn't work. I've uninstalled and reinstalled 'requests' on my server and it still doesn't work.
I've tried debugging this by seeing the verbose output from cURL but there isn't any anomaly. The cURL from my local machine and server are identical until it comes to the response part when it abruptly gives a 403 for my server's request.
I saw this but that fix is for an API hosted on their own local machine which is quite different from the production-strength API I'm connecting to.
At this point, I feel like I've tried everything - I even turned off my server and restarted all services - and haven't got anywhere. I'll be grateful if someone can even point me in the direction in which I've to go.
Solution
There isn’t any issue with my code. I figure it out after talking with my vendor.
The vendor recently started geo-fencing their allowed requests and since my server was physically located in another country it was being blocked automatically.
Answered By - regengott
0 comments:
Post a Comment
Note: Only a member of this blog may post a comment.