HTTP client
You can use the http
namespace of the Runnable API to make HTTP requests from your Runnable code. These methods are currently the only way to access the network from Runnable code.
Arbitrary socket and network access is not currently possible.
- Rust
- JavaScript/TypeScript 🧪
- AssemblyScript 🧪
- Swift 🧪
In Rust these methods are available under the http
module:
# Use the "http" module
use suborbital::http;
# Invoke the "Get" method
http::get(…)
In JavaScript and TypeScript the methods live on the http
import:
import { http } from "@suborbital/runnable"
All HTTP requests return a HttpResponse
object supporting various payload formats:
export class HttpResponse {
arrayBuffer(): ArrayBuffer {}
json(): object {}
text(): string {}
}
The headers
parameter may be specified as string header name/value pairs:
type Headers = { [key: string]: string };
In AssemblyScript all methods are prefixed with http
:
// Import then invoke "Get" method
import { httpGet } from '@suborbital/suborbital'
httpGet(…)
In Swift these methods are prefixed with Http
:
// Invoke the "Get" method
Suborbital.HttpGet(…)
note
Swift does not yet support passing headers to a request.
The following namespace methods are available:
GET​
Performs an HTTP GET request:
- Rust
- JavaScript & TypeScript
- AssemblyScript
- Swift
STATUS: STABLE
pub fn get(url: &str, headers: Option<BTreeMap<&str, &str>>) -> Result<Vec<u8>, RunErr>
STATUS: BETA
http.get(url: string, headers?: Headers): HttpResponse
STATUS: BETA
function httpGet(url: string, headers: Map<string, string> | null): ArrayBuffer
STATUS: PREVIEW
public func HttpGet(url: String) -> String
POST​
Performs an HTTP POST request:
- Rust
- JavaScript & TypeScript
- AssemblyScript
- Swift
STATUS: STABLE
pub fn post(url: &str, body: Option<Vec<u8>>, headers: Option<BTreeMap<&str, &str>>) -> Result<Vec<u8>, RunErr>
STATUS: BETA
http.post(url: string, body: string | Uint8Array, headers?: Headers): HttpResponse
STATUS: BETA
function httpPost(url: string, body: ArrayBuffer, headers: Map<string, string> | null): ArrayBuffer
STATUS: PREVIEW
public func HttpPost(url: String, body: String) -> String
PATCH​
Performs an HTTP PATCH request:
- Rust
- JavaScript & TypeScript
- AssemblyScript
- Swift
STATUS: STABLE
pub fn patch(url: &str, body: Option<Vec<u8>>, headers: Option<BTreeMap<&str, &str>>) -> Result<Vec<u8>, RunErr>
STATUS: BETA
http.patch(url: string, body: string | Uint8Array, headers?: Headers): HttpResponse
STATUS: BETA
function httpPatch(url: string, body: ArrayBuffer, headers: Map<string, string> | null): ArrayBuffer
STATUS: PREVIEW
public func HttpPatch(url: String, body: String) -> String
DELETE​
Performs an HTTP DELETE request:
- Rust
- JavaScript & TypeScript
- AssemblyScript
- Swift
STATUS: STABLE
pub fn delete(url: &str, headers: Option<BTreeMap<&str, &str>>) -> Result<Vec<u8>, RunErr>
STATUS: BETA
http.delete(url: string, headers?: Headers): HttpResponse
STATUS: BETA
function httpDelete(url: string, headers: Map<string, string> | null): ArrayBuffer
STATUS: PREVIEW
public func HttpDelete(url: String) -> String
Example Runnable​
use suborbital::runnable::*;
use suborbital::http;
use suborbital::util;
use suborbital::log;
use std::collections::BTreeMap;
struct Fetch{}
impl Runnable for Fetch {
fn run(&self, input: Vec<u8>) -> Result<Vec<u8>, RunErr> {
let url = util::to_string(input);
let _ = match http::get(url.as_str(), None) {
Ok(res) => res,
Err(e) => return Err(RunErr::new(1, e.message.as_str()))
};
// test sending a POST request with headers and a body
let mut headers = BTreeMap::new();
headers.insert("Content-Type", "application/json");
headers.insert("X-ATMO-TEST", "testvalgoeshere");
let body = String::from("{\"message\": \"testing the echo!\"}").as_bytes().to_vec();
match http::post("https://postman-echo.com/post", Some(body), Some(headers)) {
Ok(res) => {
log::info(util::to_string(res.clone()).as_str());
Ok(res)
},
Err(e) => Err(RunErr::new(1, e.message.as_str()))
}
}
}
// initialize the runner, do not edit below //
static RUNNABLE: &Fetch = &Fetch{};
#[no_mangle]
pub extern fn _start() {
use_runnable(RUNNABLE);
}