This can be done directly in python or as a file passed in. If we define our JSON directly in python we need to convert the data to json using json.dumps
If your payload is defined directly in python make sure to use python syntax (ie JSON uses "true" but python uses "True")
#Or pass in a JSON file
payload = open('example.json', 'rb').read()
Post Request
Make a post request by passing in your url, headers, and payload data if it was defined directly in python.
r = requests.post(url,headers=headers,data=payload)
Python Response
Get the response as a JSON object which can then be used as python dictionary. We first check if the query was successful. This is because error messages are strings not JSONs.
#Check response status
if r.status_code == requests.codes.ok:
#Responses are returned as JSON
response = r.json()
#Can index on JSON object like a dictionary
response_data = response["data"]
else:
#Errors are returned as text
error = r.text
Full Code
To run the code below, fill in the organization id, model id, authentication key, and file name.
import requests
import json
#User Specific inputs
organization_id = ""
model_id = ""
authentication_key = ""
file_name = ""
#Headers
headers = {
"Content-Type": "application/json",
"x-corsali-organization-id" : organization_id,
"Authorization" : "Bearer {Authentication_Key}".format(Authentication_Key=authentication_key)
}
#URL
url = "https://api.corsali.com/predict/{Model_Id}".format(Model_Id = model_id)
#JSON
payload = open(file_name, 'rb').read()
#Post Request
r = requests.post(url,headers=headers,data=payload)
#Check response status
if r.status_code == requests.codes.ok:
#Responses are returned as JSON
response = r.json()
#Can index on JSON object like a dictionary
response_data = response["data"]
print(response_data)
else:
#Errors are returned as text
error = r.text
print(error)
For a list of model ids associated with your organization see 👉