I am trying to send a request wtih post method to an API, my code looks like the following one:
import urllib.request
import json
url = "https://api.cloudflareclient.com/v0a745/reg"
referrer = "e7b507ed-5256-4bfc-8f17-2652d3f0851f"
body = {"referrer": referrer}
data = json.dumps(body).encode('utf8')
headers = {'User-Agent': 'okhttp/3.12.1'}
req = urllib.request.Request(url, data, headers)
response = urllib.request.urlopen(req)
status_code = response.getcode()
print (status_code)
Actually it works fine but i want to use "requests" library instead as it's more updated and more flexible with proxies with following code:
import requests
import json
url = "https://api.cloudflareclient.com/v0a745/reg"
referrer = "e7b507ed-5256-4bfc-8f17-2652d3f0851f"
data = {"referrer": referrer}
headers = {'User-Agent': 'okhttp/3.12.1'}
req = requests.post(url, headers=headers, json=data)
status_code = req.status_code
print (status_code)
But it returns 403 status code, how can i fix it ?
Keep in mind that this API is open to everyone and you can just run the code with no worries.
EDIT-1: i have tried removing json.dumps(body).encode('utf8')
or just .encode('utf8')
from the second code by @tomasz-wojcik advice but i am still getting 403 while the first code still works!
EDIT-2: i tried requesting with postman that successfully made the request and returned 200 status code. postman generated the following python code:
import requests
url = "https://api.cloudflareclient.com/v0a745/reg"
payload = "{\"referrer\": \"e7b507ed-5256-4bfc-8f17-2652d3f0851f\"}"
headers = {
'Content-Type': 'application/x-www-form-urlencoded',
'User-Agent': 'okhttp/3.12.1',
'Host': 'api.cloudflareclient.com'
}
response = requests.request("POST", url, headers=headers, data=payload)
status_code = response.status_code
print (status_code)
If you run the code outside of postman, it still returns 403 status code, i'm a litte confused, i am thinking that maybe "requests" library doesn't changing the user-agent in the second code.
EDIT-3: I have looked into it and found out that it works on python 2.7.16 but doesn't work on python 3.8.5!
EDIT-4: Some Developers are reporting that the second code works on python 3.6 too but the main thing is why it is working on other versions but not working on 3.8 or 3.7 ?
requests
is different from the first one – Ayannaaycock"some referrer code"
. – Tenorrhaphy