Making HTTP Requests
Klave SDK provides an interface for you to make HTTP requests from within your application in the secure hardware enclave.
Using HTTP from Applications
Klave SDK provides a simple interface to create and send HTTP request from your application.
As querying an HTTP endpoint is not a deterministic operation, it can only be used in the context of a Query. The Query calling the HTTP interface will not be callable from a Transaction.
The set of operations is the following one:
| Class | Operation | Parameters | Returns | Behavior | 
|---|---|---|---|---|
| HTTP | request | HttpRequest | HttpResponse | Execute and HttpRequestand return anHttpResponse | 
| HTTP | requestAsString | HttpRequest | HttpResponseasstring | Execute and HttpRequestand return anHttpResponseas astring | 
| HTTP | requestAsArrayBuffer | HttpRequest | HttpResponseasArrayBuffer | Execute and HttpRequestand return anHttpResponseas anArrayBuffer | 
import { Notifier, JSON, HTTP, HttpRequest } from '@klave/sdk';
import { ErrorMessage, FxRateData, FxRateResult } from './types';
/**
* @query
*/
export function grabFxRates(): void {
    const query: HttpRequest = {
        hostname: 'fx.monilytics.org',
        port: 443,
        path: '/?from=USD&to=EUR,GBP,CHF',
        headers: [],
        body: ''
    };
    const response = HTTP.request(query);
    if (!response) {
        Notifier.sendJson<ErrorMessage>({
            success: false,
            message: `HTTP call went wrong !`
        });
        return;
    }
    const ratesData = JSON.parse<FxRateData>(response.body);
    Notifier.sendJson<FxRateResult>({
        success: true,
        rates: ratesData
    });
};The set of operations is the following one:
| Class | Operation | Parameters | Returns | Behavior | 
|---|---|---|---|---|
| HTTP | request | http::Request | http::Response | Execute and http::Requestand return anhttp::Response | 
use klave;
use http::{Request, Response, StatusCode};
fn http_outcall_example(input: String) {
    let https_request = Request::builder()
        .method("POST")
        .uri(input)
        .header("Content-Type", "application/json")
        .body(String::from(""))
        .unwrap();
    let response = match klave::https::request(&https_request) {
        Ok(r) => r,
        Err(e) => {
            klave::notifier::send_string(&format!("https_query {} failure: {}", https_request.body(), e));
            return
        }
    };        
    klave::notifier::send_string(&response.body());    
}Edit on GitHub
Last updated on