I’m trying to make a request to a Cloudflare-protected endpoint using both Postman and Python’s requests library. The request succeeds in Postman, but in Python, I get a 403 error. I’ve ensured that the headers (e.g., User-Agent, Accept, etc.) in my Python script match those in Postman, but the request is still blocked.
Why does Cloudflare block my Python request while allowing Postman requests? How can I fix this?
i can achieve below output using selenium or headless selenium but i want to know why can i not do this using requests, while it can be done in postman.
when i run in postman it give result like this
{
"schemaVersion": "1.0.0",
"cg": null,
"gp": null,
"ts": null,
"cmc": null,
"ti": {
"id": "R4zj9VDraGU9JFViH7mojY",
"chain": {
"id": "solana"
},
"address": "5aBbK8HPtwH9DvXnSTXbakRW34ateooZK1bnb7qhegeZ",
"name": "Dino Game",
"symbol": "DINO",
"description": "$DINO is a one-of-a-kind GameFi meme coin that brings the nostalgia of the classic Dino Game to life while offering real-world rewards. It’s where fun meets utility, creating a revolutionary experience in the meme coin space.",
}}
the below code is generated using postman
import requests
url = "https://io.dexscreener.com/dex/pair-details/v3/solana/HKj2acbuMto92TvjtBfpPyG2im28og5b647gh1K62CAm"
payload = {}
headers = {
'User-Agent': 'Mozilla/5.0 (Windows NT 10.0; Win64; x64; rv:133.0) Gecko/20100101 Firefox/133.0',
'Cookie': '__cf_bm=Pa25nvnzsUCW1AnI5oCV6ZDLXV8HY.VjsZM1squ8D30-1735078602-1.0.1.1-7WIFetntuMUCOgKEH0XlF8uKq8e4t5sS403X8XEwzkrACUTs1woAlaXzzmN65nZksyujfMGKiD_HqyD9MRED6mASCQxtAbySJGLSyGVVvII; __cflb=04dTof7UnGZLJbSktrXU5s6TEcm2ZGkWquifmfRNx5'
}
response = requests.request("GET", url, headers=headers)
print(response.text)
- i copied url requests from chrome and paste in postman, it did not work.
- i copied url requests from firefox and paste in postman, it worked.
- what i noticed ?
firefox
- open inspection mode
- copy and paste this url “https://io.dexscreener.com/dex/pair-details/v3/solana/HKj2acbuMto92TvjtBfpPyG2im28og5b647gh1K62CAm”
- open this url in firefox –> copy value –>> copy as Curl(windows)
- open postman and paste this data and run, we recieve response with status code 200
chrome
- do above same process but recived 403 error.
to get 200 code, i untick all keys like (accept, dnt, priority, connection, accept-encoding etc…) - if we copy user-agent from firefox data and paste it in chrome it not worked unless we changed key name from user-agent to User-Agent (hear i am talking about small character to upper character)
- we need only 2 value in last to recieve 200 code in postman.
after that i generated this below code using postman. but it is not working in script.
import requests
url = "https://io.dexscreener.com/dex/pair-details/v3/solana/HKj2acbuMto92TvjtBfpPyG2im28og5b647gh1K62CAm"
payload = {}
headers = {
'User-Agent': 'Mozilla/5.0 (Windows NT 10.0; Win64; x64; rv:133.0) Gecko/20100101 Firefox/133.0',
'Cookie': 'cookie data'} ## (i changed cookie data here, we can get cookie using above mentioned mmethod)
response = requests.request("GET", url, headers=headers)
print(response.text)
1