I'm struggling to convert the below command into a python requests script. The objective is to query this API for their vehicle MOT history(UK) based on their registration, represented below as ZZ99ABC
. I've an API key and confirmed via a cURL try that my access is working.
curl -H "Accept: application/json+v6" -H "x-api-key: <my_api_key>" \https://beta.check-mot.service.gov.uk/trade/vehicles/mot-tests\?registration=ZZ99ABC
So far my attempt is producing a 403 response, missing an authentication token.
import requests
import json
reg = "ZZ99ABC"
url = f"https://beta.check-mot.service.gov.uk/trade/vehicles/mot-tests"
payload = {'registration': reg}
headers = {'x-api-key': '<my_api_key>',
'Accept': 'application/json+v6'}
r = requests.post(url, data=json.dumps(payload), headers=headers)
Any ideas where I potentially got this wrong?
Thank you in advance
requests.__version__ == '2.26.0'
python --version == 3.9.2
For any novice requests users like myself. Following @Florian C. advice and using the PreparedRequest
Class solved my query.
import requests
from requests.models import PreparedRequest
url = r"https://beta.check-mot.service.gov.uk/trade/vehicles/mot-tests"
params= {'registration': '<your_reg>'}
req = PreparedRequest()
req.prepare_url(url, params)
headers = {'x-api-key': '<your_api_key>',
'Accept': 'application/json+v6'}
r = requests.get(req.url, headers=headers)```
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.