Menu

Network and HTTP Modules

Relevant source files

The Network and HTTP Modules provide web communication capabilities for OwnLang applications. This document covers the http and okhttp modules, which offer different levels of functionality for making HTTP requests, handling responses, and managing web connections.

The http module provides a simple, straightforward interface for basic HTTP operations, while the okhttp module offers advanced features through integration with the OkHttp3 library, including multipart requests, WebSocket support, and fine-grained connection configuration.

For information about the web server capabilities, see Server Module. For general module architecture concepts, see Module Architecture.

Module Overview

The network modules implement two distinct approaches to HTTP communication:

Sources: modules/main/src/main/java/com/annimon/ownlang/modules/http/http.java1-30 modules/main/src/main/java/com/annimon/ownlang/modules/okhttp/okhttp.java1-91 docs/src/modules/http.yml1-95 docs/src/modules/okhttp.yml1-242

HTTP Module

The http module provides a simple interface for HTTP operations through four main functions: http, httpSync, download, and urlencode.

Core Functions

FunctionPurposeReturn Type
httpAsynchronous HTTP requestsBoolean (success status)
httpSyncSynchronous HTTP requestsArray [isSuccess, response]
downloadDownload content as bytesByte array
urlencodeURL encode stringsString

HTTP Function Variants

The http function supports multiple call signatures for different use cases:

Sources: modules/main/src/main/java/com/annimon/ownlang/modules/http/HttpFunctions.java53-104 docs/src/modules/http.yml7-62

Request Processing

The HTTP module processes requests through a unified pipeline that handles different parameter combinations:

Sources: modules/main/src/main/java/com/annimon/ownlang/modules/http/HttpFunctions.java106-115 modules/main/src/main/java/com/annimon/ownlang/modules/http/HttpFunctions.java117-138

Request Body Handling

The module supports different request body types based on the input parameters:

Parameter TypeBody FormatImplementation
MapValueForm dataFormBody.Builder with key-value pairs
String/OtherRaw contentRequestBody.create() with specified content type

Sources: modules/main/src/main/java/com/annimon/ownlang/modules/http/HttpFunctions.java146-184

OkHttp Module

The okhttp module provides comprehensive HTTP functionality through the OkHttp3 library, offering fine-grained control over requests, responses, and client configuration.

Module Structure

Sources: modules/main/src/main/java/com/annimon/ownlang/modules/okhttp/okhttp.java19-35 docs/src/modules/okhttp.yml162-177 docs/src/modules/okhttp.yml90-147

Request Building

The RequestBuilderValue class provides a fluent interface for constructing HTTP requests:

MethodPurposeReturns
url(url)Set request URLRequestBuilderValue
get(), post(), put(), delete()Set HTTP methodRequestBuilderValue
header(name, value)Add single headerRequestBuilderValue
headers(headersMap)Add multiple headersRequestBuilderValue
newCall(client)Create executable callCallValue

Sources: docs/src/modules/okhttp.yml90-147

Multipart Body Support

The module includes comprehensive multipart body creation through MultipartBodyBuilderValue:

Sources: modules/main/src/main/java/com/annimon/ownlang/modules/okhttp/MultipartBodyBuilderValue.java23-70 docs/src/modules/okhttp.yml68-89

Client Configuration

The HttpClientBuilderValue enables detailed HTTP client configuration:

ConfigurationPurposeTime Units Supported
callTimeoutTotal call duration limitmillis, seconds, minutes, hours, days
connectTimeoutConnection establishment limitmillis, seconds, minutes, hours, days
readTimeoutResponse reading limitmillis, seconds, minutes, hours, days
writeTimeoutRequest writing limitmillis, seconds, minutes, hours, days
retryOnConnectionFailureRetry failed connectionsboolean

Sources: modules/main/src/main/java/com/annimon/ownlang/modules/okhttp/HttpClientBuilderValue.java18-47 docs/src/modules/okhttp.yml216-240

WebSocket Support

The module provides WebSocket functionality through callback-based event handling:

Sources: modules/main/src/main/java/com/annimon/ownlang/modules/okhttp/HttpClientValue.java50-123 docs/src/modules/okhttp.yml196-198

Request Body Types

The RequestBodyValue supports multiple content types:

MethodContent SourceParameters
bytesByte arraycontentType, bytes, offset, count
fileFile systemcontentType, filePath
stringString contentcontentType, content

Sources: modules/main/src/main/java/com/annimon/ownlang/modules/okhttp/okhttp.java37-75 docs/src/modules/okhttp.yml148-161

Module Integration

Both modules integrate with OwnLang's module system through the standard Module interface:

The http module exposes only functions, while the okhttp module provides both constants (factory objects) and maintains an empty functions map, relying on the constant objects to provide callable functionality.

Sources: modules/main/src/main/java/com/annimon/ownlang/modules/http/http.java15-29 modules/main/src/main/java/com/annimon/ownlang/modules/okhttp/okhttp.java18-35