Network utilities

GreyCat offers some network utilities to communicate with the outside world.
They are all located in the io package, so you need to add the use io; directive to access these functions.


APIs are common ways to interact between different system. GreyCat provides means to perform GET, POST and PUT requests. All functions give offer the possibility to specify headers.


Http::get requires the http endpoint as first parameter in form of a String, second parameter (nullable) is an array of HttpHeader. This second parameter can come handy when one need to specify a token_bearer or content-type for instance.
The body of the response is returned in the result of the function call.

use io;

fn call() {
    var googlePage = Http::get("", null);
    var myData = Http::get("", [HttpHeader{name: "Accept", value: "application/json"}, HttpHeader{name: "Bearer", value: "${myToken}"}]);


Http::getFile is very similar to Http::get, but rather writes the body of the response into a File instead of returning the content in a string. There is therefore an additional parameter to specify the path of the file you want the result to be writen.

use io;

fn call() {
    var filePath = "./data/file-${time::now().to(DurationUnit::milliseconds)}.json";
    Http::getFile("", filePath, null);
    var content = JsonReader::new(filePath);


Http::post and Http::put have similar parameters. The first parameter is the endpoint (String), the second is the content to be sent in the body of the request, and finally the list of headers, if any.
The data is encoded in JSON. The example shows how a Http:post request is made with params, a content, and a return value parsed as JSON from the String.

use io;

fn call() {
    var endpoint = "${base_endpoint}/datafetch?api_key=${this.api_key}&user_id=${this.user_id}";
    var request = {
        sampling: ["live"],
        structure_uuids: [structureUuid]
    var result = Http::post(endpoint, request, this.defaultHeaders());
    var parsedResult = JsonReader::parse(result as String);

When using Http::post to issue HTTP requests to GreyCat endpoints (when communicating to a different GreyCat server, or for testing):


Sending an email is possible, using the Smtp type.

SMTP Connection

You need to create an Smtp object that will specify the details of teh server you target.
Once done with the configuration, you can call send with the email content you want to transmit.

use io;

fn call() {
    var smtpServer = Smtp{
        host: "",
        port: 587,
        mode: SmtpMode::starttls,
        authenticate: SmtpAuth::login,
        user: "it_s_me",
        pass: "my_pass"


The Email obejct allows to specify the various fields of the email. Recipients, subject, content, etc.

var myEmail = Email{
    from: "\"John DOE\" <>",
    to: ["\"John BOSS\" <>"],
    cc: [
        "\"John LEFTBUDDY\" <>",
        "\"John RIGHTBUDDY\" <>",
    bcc: null,
    body_is_html: false, //Text only
    subject: "Important things",
    body: "This is a text content.\n\nBest regards\nJohn",

This email can then be sent using smtpServer.send(myEmail);


It is sometimes necessary to extract parts of URLs. This utility type helps parsing and producing URLs.

var theUrl = Url::parse("");