So when I access the API target via Postman with the URL below, it works fine without any issues
base_url = https://api.cats.net/orgs/CatEmpire/audit-log?phrase=action:stuck_on_tree date_of_event:2022-01-11
However, when I append the below parameters into my request, the URL comes out differently and I'm no longer able to get results
parameters = {
'action:': 'stuck_on_tree',
'date_of_event:': '2022-01-11'
}
PAT = asdasdhdhdhhd123123
response = requests.get(base_url, headers={"Accept": "application/vnd.github.v3 json", "Authorization": f"Bearer {PAT}"}, params=parameters)
print(response.request.url)
#This returns https://api.cats.net/orgs/CatEmpire/audit-log?phrase=&action:=stuck_on_tree&date_of_event:=2022-01-11
I have tried to use:
paramters_string = urllib.parse.urlencode(parameters, safe='')
And then I updated my response variable below, but the results are still exaxtly the same. I have tried to do some digging but I can't seem to figure out if this an issue because I'm using a dictionary to pass the params, or if there's something else that I'm not able to understand. I'm fairly new to Python.
`response = requests.get(base_url, headers={"Accept": "application/vnd.github.v3 json", "Authorization": f"Bearer {PAT}"}, params=parameters)`
CodePudding user response:
Your base URL should not include part of your query string (?phrase=).
Use this for your base URL:
https://api.cats.net/orgs/CatEmpire/audit-log
For your parameters, use this:
parameters = {
'phrase': 'action:stuck_on_tree date_of_event:2022-01-11'
}
Update
Since you can't URL encode your parameters due to API constraints, you'll have to pass them as a string like so:
parameters = 'phrase=action:stuck_on_tree date_of_event:2022-01-11'
CodePudding user response:
have you tried ?
PAT = "asdasdhdhdhhd123123"
data = parameters
this is just an example of authorization request:
import requests
endpoint = ".../api/ip"
data = {"ip": "1.1.2.3"}
headers = {"Authorization": "Bearer MYREALLYLONGTOKENIGOT"}
response = requests.post(endpoint, data=data, headers=headers)
