API Call from RPA

I need to make a call to an API from RPA and it looks like I can do it from the ‘requests’ module but I can’t import it. I have tried using PIP to install it but I still get the same error message when trying to import requests. “ModuleNotFoundError: No module named ‘requests’”

This is the code that I want to use…
import requests
from requests.structures import CaseInsensitiveDict

url = “https://beta.check-mot.service.gov.uk/trade/vehicles/mot-tests?registration=‘VehicleReg’

headers = CaseInsensitiveDict()
headers[“Accept”] = “application/json+v6”
headers[“x-api-key”] = “‘API Key’”

resp = requests.get(url, headers=headers)

print(resp.status_code)

In case anyone else needs it the answer to the question was to use ‘import urllib’ which is included with Liberty RPA and is the way to make API calls, that led to a solution.

Originally this was a curl command…

curl -H “Accept: application/json+v6” -H “x-api-key: [DR_Key]” \https://beta.check-mot.service.gov.uk/trade/vehicles/mot-tests?registration=[car_reg]

In a Python Code in Liberty RPA it is now…

"import http.client
import json

conn = http.client.HTTPSConnection(“beta.check-mot.service.gov.uk”)

headers = {
‘Accept’: “application/json+v6”,
‘x-api-key’: “[DR_Key]”
}

conn.request(“GET”, “/trade/vehicles/mot-tests?registration=[car_reg]”, headers=headers)

res = conn.getresponse()
result = json.loads(res.read().decode(‘utf-8’))[0]"

This worked for me.

1 Like