To send requests to the server, you can use BPL operations in the Expression block. This allows you to use an extended list of HTTP request methods and work more flexibly with the server response through other BPL operations.
Use the http.request function to send HTTP requests. Unlike http.sendRequest, it provides more options, such as setting headers, setting a timeout, and conveniently forming the request body.
Method for sending an HTTP request:
http.request(url string = “”, method string = “POST”, body any = nil) Request
Purpose: forms an HTTP request with the ability to flexibly configure parameters before sending.
Arguments:
url — a string with the API address.method — HTTP method (GET, POST, PUT, DELETE, PATCH, HEAD, OPTIONS).body — request body content.Return value: an object containing information about the request, which can be further configured before sending.
If you need to pass arguments in the URL, use the :: operator to concatenate strings. For example, to pass a phone number in the URL:
$url = “http://rosreestr.subnets.ru/?get=num&format=json&num=” :: $phone_number;
$response = http.request($url, “GET”).send()
If you need to pass data in the request body, such as JSON, use the following format:
$json = {“instances”:[{‘text’: “Example text for processing.”}]};
$response = http.request(“https://api.aicloud.sbercloud.ru/public/v2/rewriter/predict”, “POST”, $json)
.headers({“accept”: “application/json”, “Content-Type”: “application/json”})
.send()
When sending requests that require authorization, you must pass the token in the Authorization header:
$response = http.request(“https://iam.twin-ai.com/api/v1/users”, “GET”, {‘limit’: 1, “offset”: 3})
.headers({“Authorization”: “Bearer YOUR_TOKEN”})
.send()
After sending the request, the server response is written to the variable specified when calling the method. Before processing the response, it is important to check its correctness.
Correct response processing:
Save the server response to a variable.
Get the response code and possible error message in the same block.
Add a Condition block and check:
Example of retrieving the count value from the response body:
$body = $response.body
$count = $body.get(“count”)
In many API responses, data is presented as nested JSON structures. To conveniently extract values from such structures, use the .get() method, which allows you to access nested elements by their keys.
Consider the following example of JSON data:
{
“version”: “1.0”,
“timestamp”: “2025-03-04T12:00:00Z”,
“source”: “generated_data”,
“items”: [
{
“email”: “test1@mail.com”,
“name”: “John Doe”,
“age”: 28,
“country”: “USA”,
“roles”: [
{“name”: “admin”},
{“name”: “editor”}
]
},
{
“email”: “test2@mail.com”,
“name”: “Jane Smith”,
“age”: 34,
“country”: “Canada”,
“roles”: [
{“name”: “user”}
]
}
]
}
Example of extracting the email of the first element from the items array:
$email_address = $response.body.get(“items”).get(‘0’).get(“email”)
Example of obtaining the name from the roles array:
$role_name = $response.body.get(“items”).get(“0”).get(“roles”).get(‘0’).get(“name”)
This code extracts the value of the name field of the first object inside the roles array of the first element of items, which in this case corresponds to “admin”.