Request Setup
The run-analysis api accepts gzip compressed and base64 encoded data in the payload. In the Analyses Settings chapter it was disscused that the various analyses settings are JSON objects. In order to send the settings to the run-analysis API, they need to be stringified, compressed and encoded.
See the following code sample:
- Python
- C#
import gzip, base64
import json
import requests
analysis_settings={} # The Analyses Setting object goes here
json_string=json.dumps(analysis_settings)
# Compress the JSON string using Gzip and then base64
compressed_data = gzip.compress(json_string.encode('utf-8'))
base64_encoded = base64.b64encode(compressed_data).decode('utf-8')
# Use the base64_encoded string to your request
# URL for the request
url = "<run-analysisURL>" # See API Reference chapter
# Headers
headers = {
"x-api-key":"<YOUR_API_KEY>", # your API KEY goes here
"Content-Type": "text/plain",
"X-Infrared-Encoding": "gzip",
}
# Perform the request
response = requests.post(url, data=base64_encoded, headers=headers)
using System;
using System.IO;
using System.IO.Compression;
using System.Net.Http;
using System.Text;
using System.Threading.Tasks;
public class ApiClient
{
private static readonly HttpClient client = new HttpClient();
public async Task<HttpResponseMessage> SendCompressedJsonAsync(string jsonString, string apiKey)
{
// Compress the JSON string using Gzip and then base64
string base64Encoded;
using (var memoryStream = new MemoryStream())
{
using (var gzipStream = new GZipStream(memoryStream, CompressionMode.Compress))
using (var writer = new StreamWriter(gzipStream, Encoding.UTF8))
{
writer.Write(jsonString);
}
var compressedData = memoryStream.ToArray();
base64Encoded = Convert.ToBase64String(compressedData);
}
// URL for the request
var url = "<run-analysisURL>"; # See API Reference chapter
// Create the request message
var request = new HttpRequestMessage(HttpMethod.Post, url)
{
Content = new StringContent(base64Encoded, Encoding.UTF8, "text/plain")
};
// Add headers
request.Headers.Add("x-api-key", apiKey);
request.Headers.Add("X-Infrared-Encoding", "gzip");
// Send the request
return await client.SendAsync(request);
}
}