Network Module
The Language::Network module provides HTTP client and server capabilities for network communication.
xxml
#import Language::Network;HTTPClient
HTTP client for making web requests.
Quick Methods
| Method | Parameters | Returns | Description |
|---|---|---|---|
| get | url: String^ | HTTPResponse^ | HTTP GET request |
| post | url: String^, body: String^ | HTTPResponse^ | HTTP POST request |
| put | url: String^, body: String^ | HTTPResponse^ | HTTP PUT request |
| delete | url: String^ | HTTPResponse^ | HTTP DELETE request |
| patch | url: String^, body: String^ | HTTPResponse^ | HTTP PATCH request |
xxml
// Simple GET request
Instantiate Network::HTTPResponse^ As <response> = Network::HTTPClient::get(
String::Constructor("https://api.example.com/users")
);
If (response.isSuccess().toBool())
{
Instantiate String^ As <body> = response.getBody();
Run System::Console::printLine(body);
}
// POST with JSON body
Instantiate Network::HTTPResponse^ As <postResponse> = Network::HTTPClient::post(
String::Constructor("https://api.example.com/users"),
String::Constructor("{\"name\": \"Alice\"}")
);Configuration
| Method | Parameters | Returns | Description |
|---|---|---|---|
| setHeader | name: String^, value: String^ | HTTPClient^ | Set request header |
| setTimeout | ms: Integer^ | HTTPClient^ | Set timeout in milliseconds |
| send | — | HTTPResponse^ | Execute configured request |
xxml
// Configured request with headers
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));
// Make the request
Instantiate Network::HTTPResponse^ As <response> = client.post(
String::Constructor("https://api.example.com/data"),
String::Constructor("{\"key\": \"value\"}")
);HTTPResponse
Represents an HTTP response from a server.
| Method | Parameters | Returns | Description |
|---|---|---|---|
| getStatusCode | — | Integer^ | HTTP status code (200, 404, etc.) |
| getBody | — | String^ | Response body as string |
| getHeader | name: String^ | String^ | Get specific header value |
| getHeaders | — | List<String>^ | Get all header names |
| isSuccess | — | Bool^ | Check if status is 2xx |
xxml
Instantiate Network::HTTPResponse^ As <response> = Network::HTTPClient::get(
String::Constructor("https://api.example.com/status")
);
// Check status
Instantiate Integer^ As <status> = response.getStatusCode();
Run System::Console::printLine(
String::Constructor("Status: ").append(status.toString())
);
// Check if successful (2xx)
If (response.isSuccess().toBool())
{
// Get body
Instantiate String^ As <body> = response.getBody();
Run System::Console::printLine(body);
// Get specific header
Instantiate String^ As <contentType> = response.getHeader(
String::Constructor("Content-Type")
);
Run System::Console::printLine(
String::Constructor("Content-Type: ").append(contentType)
);
}
Else
{
Run System::Console::printLine(String::Constructor("Request failed!"));
}Note
Common HTTP status codes: 200 (OK), 201 (Created), 400 (Bad Request), 401 (Unauthorized), 404 (Not Found), 500 (Server Error).
HTTPServer
Simple HTTP server for handling incoming requests.
| Method | Parameters | Returns | Description |
|---|---|---|---|
| Constructor | port: Integer^ | HTTPServer^ | Create server on port |
| route | path: String^, handler: Lambda | HTTPServer^ | Register route handler |
| start | — | None | Start listening |
| stop | — | None | Stop server |
| isRunning | — | Bool^ | Check if running |
xxml
// Create server on port 8080
Instantiate Network::HTTPServer^ As <server> = Network::HTTPServer::Constructor(
Integer::Constructor(8080)
);
// Register routes
Run server.route(
String::Constructor("/"),
Lambda (request: Request&) -> Response^ {
Return Response::text(String::Constructor("Hello, World!"));
}
);
Run server.route(
String::Constructor("/api/status"),
Lambda (request: Request&) -> Response^ {
Return Response::json(String::Constructor("{\"status\": \"ok\"}"));
}
);
// Start server
Run System::Console::printLine(String::Constructor("Starting server on port 8080..."));
Run server.start();Warning
The server runs synchronously. For production use, consider running the server in a separate thread using the Concurrent module.
Complete Example
http_client.xxml
1 #import Language::Core; 2 #import Language::Network; 3 #import Language::Format; 4 #import Language::System; 5 6 [ Entrypoint 7 { 8 Run System::Console::printLine(String::Constructor("=== HTTP Client Demo ===")); 9 10 // GET request to a public API 11 Instantiate String^ As <url> = String::Constructor( 12 "https://jsonplaceholder.typicode.com/posts/1" 13 ); 14 15 Run System::Console::printLine( 16 String::Constructor("Fetching: ").append(url) 17 ); 18 19 Instantiate Network::HTTPResponse^ As <response> = Network::HTTPClient::get(url); 20 21 Run System::Console::printLine( 22 String::Constructor("Status: ").append(response.getStatusCode().toString()) 23 ); 24 25 If (response.isSuccess().toBool()) 26 { 27 Run System::Console::printLine(String::Constructor("")); 28 Run System::Console::printLine(String::Constructor("Response:")); 29 30 // Parse JSON response 31 Instantiate Format::JSONObject^ As <post> = Format::JSONObject::parse( 32 response.getBody() 33 ); 34 35 Instantiate String^ As <title> = post.getString(String::Constructor("title")); 36 Instantiate Integer^ As <userId> = post.getInt(String::Constructor("userId")); 37 38 Run System::Console::printLine( 39 String::Constructor(" Title: ").append(title) 40 ); 41 Run System::Console::printLine( 42 String::Constructor(" User ID: ").append(userId.toString()) 43 ); 44 } 45 Else 46 { 47 Run System::Console::printLine(String::Constructor("Request failed!")); 48 } 49 50 // POST example 51 Run System::Console::printLine(String::Constructor("")); 52 Run System::Console::printLine(String::Constructor("=== POST Request ===")); 53 54 Instantiate Format::JSONObject^ As <newPost> = Format::JSONObject::Constructor(); 55 Run newPost.put(String::Constructor("title"), String::Constructor("My Post")); 56 Run newPost.put(String::Constructor("body"), String::Constructor("Post content here")); 57 Run newPost.put(String::Constructor("userId"), Integer::Constructor(1)); 58 59 Instantiate Network::HTTPClient^ As <client> = Network::HTTPClient::Constructor(); 60 Run client.setHeader( 61 String::Constructor("Content-Type"), 62 String::Constructor("application/json") 63 ); 64 65 Instantiate Network::HTTPResponse^ As <postResponse> = client.post( 66 String::Constructor("https://jsonplaceholder.typicode.com/posts"), 67 newPost.toString() 68 ); 69 70 Run System::Console::printLine( 71 String::Constructor("POST Status: ").append(postResponse.getStatusCode().toString()) 72 ); 73 74 If (postResponse.isSuccess().toBool()) 75 { 76 Run System::Console::printLine(String::Constructor("Post created successfully!")); 77 } 78 79 Exit(0); 80 } 81 ]