HTTP

HTTP client and server functionality for web requests. The Network module provides HTTPClient, HTTPResponse, and HTTPServer classes.

xxml
#import Language::Core;
#import Language::Network;

Note

HTTP functionality requires C++ runtime backing and may need external libraries (e.g., libcurl) for full implementation.

HTTPResponse

Represents an HTTP response.

Constructor

MethodParametersReturnsDescription
ConstructorHTTPResponse^Create response object

Response Methods

MethodParametersReturnsDescription
getStatusCodeInteger^Status code (200, 404, etc.)
getBodyString^Response content
isSuccessBool^True for 200-299
isErrorBool^True for 400+
xxml
If (response.isSuccess()) -> {
    Run Console::printLine(String::Constructor("Request succeeded"));
    Run Console::printLine(response.getBody());
}

If (response.isError()) -> {
    Run Console::printError(String::Constructor("Request failed: ").append(response.getStatusCode().toString()));
}

HTTPClient

HTTP client for making web requests.

Constructor

MethodParametersReturnsDescription
ConstructorHTTPClient^Create HTTP client

Configuration

MethodParametersReturnsDescription
setHeaderkey: String^, value: String^NoneSet request header
setTimeoutmilliseconds: Integer^NoneSet timeout
xxml
Instantiate Network::HTTPClient^ As <client> = Network::HTTPClient::Constructor();
Run client.setHeader(String::Constructor("Content-Type"), String::Constructor("application/json"));
Run client.setHeader(String::Constructor("Authorization"), String::Constructor("Bearer token123"));
Run client.setTimeout(Integer::Constructor(5000));  // 5 seconds

HTTP Methods

MethodParametersReturnsDescription
performGeturl: String^HTTPResponse^GET request
performPosturl: String^, data: String^HTTPResponse^POST request
xxml
// GET request
Instantiate Network::HTTPResponse^ As <response> = client.performGet(
    String::Constructor("https://api.example.com/data")
);

// POST request with JSON body
Instantiate Network::HTTPResponse^ As <postResp> = client.performPost(
    String::Constructor("https://api.example.com/submit"),
    String::Constructor("{"name":"Alice"}")
);

HTTPServer

Simple HTTP server (experimental).

Constructor

MethodParametersReturnsDescription
ConstructorHTTPServer^Create HTTP server

Server Control

MethodParametersReturnsDescription
listenport: Integer^NoneStart listening
stopNoneStop server
isRunningBool^True if running
xxml
Instantiate Network::HTTPServer^ As <server> = Network::HTTPServer::Constructor();
Run server.listen(Integer::Constructor(8080));

If (server.isRunning()) -> {
    Run Console::printLine(String::Constructor("Server is running on port 8080"));
}

// Later...
Run server.stop();

Status Code Ranges

RangeCategoryExample
200-299Success200 OK, 201 Created
300-399Redirect301 Moved, 304 Not Modified
400-499Client Error400 Bad Request, 404 Not Found
500-599Server Error500 Internal Error, 503 Unavailable

Complete Example

http_example.xxml
1#import Language::Core;
2#import Language::Network;
3#import Language::Format;
4#import Language::System;
5
6[ Entrypoint
7 {
8 // Create HTTP client
9 Instantiate Network::HTTPClient^ As <client> = Network::HTTPClient::Constructor();
10
11 // Configure client
12 Run client.setHeader(String::Constructor("Accept"), String::Constructor("application/json"));
13 Run client.setTimeout(Integer::Constructor(10000)); // 10 second timeout
14
15 // Make GET request
16 Run Console::printLine(String::Constructor("Making GET request..."));
17 Instantiate Network::HTTPResponse^ As <getResp> = client.performGet(
18 String::Constructor("https://api.example.com/users/1")
19 );
20
21 If (getResp.isSuccess()) -> {
22 Run Console::printLine(String::Constructor("GET succeeded"));
23 Run Console::printLine(String::Constructor("Body: ").append(getResp.getBody()));
24 }
25
26 // Make POST request with JSON
27 Instantiate Format::JSONObject^ As <postData> = Format::JSONObject::Constructor();
28 Run postData.set(String::Constructor("name"), String::Constructor("Alice"));
29 Run postData.set(String::Constructor("email"), String::Constructor("alice@example.com"));
30
31 Run client.setHeader(String::Constructor("Content-Type"), String::Constructor("application/json"));
32
33 Instantiate Network::HTTPResponse^ As <postResp> = client.performPost(
34 String::Constructor("https://api.example.com/users"),
35 postData.stringify()
36 );
37
38 If (postResp.isSuccess()) -> {
39 Run Console::printLine(String::Constructor("POST succeeded"));
40 Run Console::printLine(String::Constructor("Status: ").append(postResp.getStatusCode().toString()));
41 }
42
43 If (postResp.isError()) -> {
44 Run Console::printError(String::Constructor("POST failed with status: ").append(postResp.getStatusCode().toString()));
45 }
46
47 Exit(0);
48 }
49]

See Also