Example
HTTP, JSON & errors
including the standard library: serialize a body, POST and GET, catch whatever explodes.
The standard library ships with the official Alana download. Save as a .alana file, then alana filename.alana.
including "stdlib/http.alana";
including "stdlib/json.alana";
including "stdlib/error.alana";
define Http http is newobj Http();
define Json json is newobj Json();
define func Serialize(any value) returning str;
return call json.Serialize(value);
end;
define func SendRequest(str method, str url, str jsonBody) returning str;
if method sameas "GET";
return call http.Get(url).GetBody();
elseif method sameas "POST";
return call http.Post(url, jsonBody, "application/json").GetBody();
else;
return "";
end;
end;
define str body is "{
\"name\": \"John Doe\",
\"age\": 30,
\"email\": \"johndoe@example.com\"
}";
define str parsedBody is call Serialize(body);
while true;
try;
showln "---- Sending POST Request----";
showln;
showln "Press Enter to send a POST request to https://postman-echo.com/post";
readln;
showln call SendRequest("POST", "https://postman-echo.com/post", parsedBody);
showln "---- Sending GET Request----";
showln;
showln "Press Enter to send a GET request to https://postman-echo.com/get";
readln;
showln call SendRequest("GET", "https://postman-echo.com/get?phrase=HelloWorld", parsedBody);
end;
catch(error);
throw newobj Error(error);
end;
end;