HTTP Client

Make HTTP requests with get, post, and fetch. Streaming reads for SSE and chunked responses. TLS is automatic for https URLs.

imp std/http { get, post, fetch, stream_open, stream_read, stream_close }
imp std/io { print, println }
imp std/json

fn main() {
  // simple GET - returns { status, headers, body }
  res = get("https://httpbin.org/get")
  println("status: {res.status}")

  // POST with JSON body
  res = post("https://httpbin.org/post", {
    body: json.encode({ name: "pyr" }),
    headers: "Content-Type: application/json"
  })
  data = json.decode(res.body)
  println("echoed: {getattr(data, "data")}")

  // full control with fetch - any HTTP method
  res = fetch({
    url: "https://httpbin.org/put",
    method: "PUT",
    headers: "Content-Type: text/plain",
    body: "hello"
  })
  println("put status: {res.status}")

  // streaming - read chunks as they arrive
  reader = stream_open({
    url: "https://httpbin.org/stream/3",
    method: "GET"
  })
  initial = getattr(reader, "initial")
  if len(initial) > 0 { print(initial) }
  while true {
    chunk = stream_read(reader)
    if chunk == nil { break }
    print(chunk)
  }
  stream_close(reader)
}