Why can't I access an API with Python, but I can do it with my browser?
Question:
I want to access an API with Python but it’s not working. I get this JSON file:
{"status": 403, "message": "Forbidden"}
My code:
result = requests.get(f"https://api.henrikdev.xyz/valorant/v2/match/06a29aa4-7862-4d6b-85f7-1fda200386f1").json()
with open("result.json", "w") as f:
json.dump(result, f)
But when I access the API on my browser it works: https://api.henrikdev.xyz/valorant/v2/match/06a29aa4-7862-4d6b-85f7-1fda200386f1
Answers:
It’s working smoothly. You have to just inject user-agent as header
import requests
import json
headers={'User-Agent':'mozilla/5.0'}
result = requests.get(f"https://api.henrikdev.xyz/valorant/v2/match/06a29aa4-7862-4d6b-85f7-1fda200386f1",headers=headers).json()
jdata = json.dumps(result,indent=4)
with open("result.json", "w") as f:
f.write(jdata)
I used your same example but I am getting an error
Can you tell me what happens?
"message": "The application has encountered an error",
"type": "NullPointerError"
import requests
import json
import urllib.parse
query = "SELECT min({cart_pai.pk}) FROM {cart as carrinho JOIN cart AS cart_pai ON {carrinho.parent} = {cart_pai.pk}}"
safe_string = urllib.parse.quote_plus(query)
headers = {
'Accept': 'application/json',
'Authorization': 'Bearer 4DmQkomKs0z845705JIlfW-hhNg'}
result = requests.get(f"https://api.cokecxf-commercec1-p1-public.model-t.cc.commerce.ondemand.com/clarowebservices/v2/claro/flexiblesearch/export/search/getAsCsv?"+safe_string,
headers=headers).json()
jdata = json.dumps(result, indent=4)
with open("result.json", "w") as f:
f.write(jdata)
print(jdata)
I want to access an API with Python but it’s not working. I get this JSON file:
{"status": 403, "message": "Forbidden"}
My code:
result = requests.get(f"https://api.henrikdev.xyz/valorant/v2/match/06a29aa4-7862-4d6b-85f7-1fda200386f1").json()
with open("result.json", "w") as f:
json.dump(result, f)
But when I access the API on my browser it works: https://api.henrikdev.xyz/valorant/v2/match/06a29aa4-7862-4d6b-85f7-1fda200386f1
It’s working smoothly. You have to just inject user-agent as header
import requests
import json
headers={'User-Agent':'mozilla/5.0'}
result = requests.get(f"https://api.henrikdev.xyz/valorant/v2/match/06a29aa4-7862-4d6b-85f7-1fda200386f1",headers=headers).json()
jdata = json.dumps(result,indent=4)
with open("result.json", "w") as f:
f.write(jdata)
I used your same example but I am getting an error
Can you tell me what happens?
"message": "The application has encountered an error",
"type": "NullPointerError"
import requests
import json
import urllib.parse
query = "SELECT min({cart_pai.pk}) FROM {cart as carrinho JOIN cart AS cart_pai ON {carrinho.parent} = {cart_pai.pk}}"
safe_string = urllib.parse.quote_plus(query)
headers = {
'Accept': 'application/json',
'Authorization': 'Bearer 4DmQkomKs0z845705JIlfW-hhNg'}
result = requests.get(f"https://api.cokecxf-commercec1-p1-public.model-t.cc.commerce.ondemand.com/clarowebservices/v2/claro/flexiblesearch/export/search/getAsCsv?"+safe_string,
headers=headers).json()
jdata = json.dumps(result, indent=4)
with open("result.json", "w") as f:
f.write(jdata)
print(jdata)