Skip to main content

Module internet

Module internet 

Source
Expand description

HTTP(S) requests and TCP connections through an internet card.

use std::io::Read;
use wackasm_opencomputers::internet;

let mut response = internet::get("https://example.com/")?;
println!("{} {}", response.status(), response.message());
let mut body = String::new();
response.read_to_string(&mut body)?;

// Or build the request.
let response = internet::Request::new("https://httpbin.org/post")
    .method("POST")
    .header("Content-Type", "application/json")
    .body(r#"{"hello": "world"}"#)
    .send()?;

The card does the networking on the server. HTTP and TCP have to be enabled in OpenComputers’ config (they are by default), which also limits which hosts can be reached; by default, local and private addresses are blocked. Reads come in chunks of at most 2 KiB (maxReadBuffer), one per server tick, so expect about 40 KB/s.

Response and TcpStream are std::io streams. Dropping one closes the connection and releases it on the host.

Structs§

Request
An HTTP request to send.
Response
An HTTP response. Reading it reads the body.
TcpStream
A TCP connection.

Functions§

get
A shortcut for Request::new(url).send().
get_async
internet::get(url) without blocking; see Request::send_async.