Request examples¶
Because it might be difficult to figure out how to make a request from the framework you are using, we have collected a few examples here for various languages.
Below you can check some examples for creating a direct request to the default version of a deployment and to a pipeline.
API endpoints
See this page on API endpoints to find the right endpoint URLs to use with the examples below.
Making a direct request to the default version of a deployment with the Python or R client library:
# Using the Python Client Library
import ubiops
client = ubiops.ApiClient(ubiops.Configuration(api_key={"Authorization": API_TOKEN}))
api = ubiops.CoreApi(client)
data = {'input_field_1': 123}
# A direct request to the default version
request_result = api.deployment_requests_create(
project_name=PROJECT_NAME,
deployment_name=DEPLOYMENT_NAME,
data=data
)
print(request_result)
# Using the R Client Library
Sys.setenv(UBIOPS_PROJECT = "<YOUR_PROJECT_NAME>")
Sys.setenv(UBIOPS_API_TOKEN = "Token <YOUR_API_TOKEN>")
library(ubiops)
input_data <- list(input_field_1 = 123)
result <- deployment_requests_create(
deployment.name = DEPLOYMENT_NAME,
data = input_data
)
print(result)
The same, now using generic methods:
# Using a 'raw' HTTP request
import requests
# You can copy the endpoint URL from the WebApp or look up the format on Swagger
receive = requests.post("<deployment endpoint url>/requests",
json = {'input field 1': 123},
headers={'Authorization':'your API token'})
print(receive.json()['result'])
# Using a 'raw' HTTP request
content_type <- httr::content_type_json()
body <- rjson::toJSON(list(input_field_1 = 123))
headers <- httr::add_headers(Authorization="Token <YOUR_API_TOKEN>")
r <- httr::POST("<deployment endpoint url>/requests", headers, content_type, body = body, encode = "json")
content <- httr::content(r, "text", encoding = "UTF-8")
print(content)
// Helper function for posting a request
async function postRequest(url = "", data = {}) {
const response = await fetch(url, {
method: "POST",
headers: {
"Content-Type": "application/json",
Authorization: API_TOKEN,
},
body: JSON.stringify(data),
});
return response.json();
}
// Using the postRequest function
postRequest(
`<deployment endpoint url>/requests`,
{ input_field_1: 123 }
).then((response) => doSomethingWith(response))
Making a direct request to the default version of a pipeline with the Python or R client library:
# Using the Python Client Library
import ubiops
client = ubiops.ApiClient(ubiops.Configuration(api_key={"Authorization": API_TOKEN}))
api = ubiops.CoreApi(client)
data = {'input_field_1': 123}
request_result = api.pipeline_requests_create(
project_name=PROJECT_NAME,
pipeline_name=PIPELINE_NAME,
data=data
)
print(request_result)
# Using the R Client Library
Sys.setenv(UBIOPS_PROJECT = "<YOUR_PROJECT_NAME>")
Sys.setenv(UBIOPS_API_TOKEN = "Token <YOUR_API_TOKEN>")
library(ubiops)
input_data <- list(input_field_1 = 123)
result <- pipeline_requests_create(
pipeline.name = PIPELINE_NAME,
data = input_data
)
print(result)
The same, now using generic methods:
# Using a 'raw' HTTP request
import requests
# You can copy the endpoint URL from the WebApp or look up the format on Swagger
receive = requests.post(
url='<pipeline endpoint url>/requests',
json={'input field 1': 123},
headers={'Authorization':'your API token'}
)
print(receive.json()['result'])
# Using a 'raw' HTTP request
content_type <- httr::content_type_json()
body <- rjson::toJSON(list(input_field_1 = 123))
headers <- httr::add_headers(Authorization="Token <YOUR_API_TOKEN>")
r <- httr::POST("<pipeline endpoint url>/requests", headers, content_type, body = body, encode = "json")
content <- httr::content(r, "text", encoding = "UTF-8")
print(content)
// Helper function for posting a request
async function postRequest(url = "", data = {}) {
const response = await fetch(url, {
method: "POST",
headers: {
"Content-Type": "application/json",
Authorization: API_TOKEN,
},
body: JSON.stringify(data),
});
return response.json();
}
// Using the postRequest function
postRequest(
`<pipeline endpoint url>/requests`,
{ input_field_1: 123 }
).then((response) => doSomethingWith(response))