Skip to content

http

HTTP/1.1 client for making web requests. Built on POSIX sockets — works on PC and ESP32. Default Content-Type for POST/PUT is application/json.

Version v0.1
Platform PC, ESP32
Type Native (C)

star.mod

require dev-libs/http v0.1

Usage

import "http"

Functions

Quick Methods

Function Signature Description
http.get(url) (string) -> dict HTTP GET request
http.post(url, body) (string, string) -> dict HTTP POST with body
http.put(url, body) (string, string) -> dict HTTP PUT with body
http.delete(url) (string) -> dict HTTP DELETE request
var res: dict = http.get("http://api.example.com/data")
console.log(res["status"])  # 200
console.log(res["body"])    # response body as string

Full Control

Function Signature Description
http.request(method, url, body, headers, timeoutMs) (string, string, string, dict, i32) -> dict Custom request with headers and timeout
var headers: dict = {"Authorization": "Bearer token123", "Content-Type": "text/plain"}
var res: dict = http.request("PATCH", "http://api.example.com/item/1", "data", headers, 5000)

Pass 0 for timeoutMs to use the default (10 seconds).

Response Format

All functions return a dict with:

Key Type Description
"status" number HTTP status code (0 = connection failed)
"body" string Response body
var res: dict = http.get("http://example.com")
if (res["status"] == 200) {
    console.log(res["body"])
}

Pattern: POST Sensor Data

package main

import "http"
import "json"

fn main():void {
    var payload: dict = {"sensor": "temp", "value": 23.5}
    var body: string = json.stringify(payload)

    var res: dict = http.post("http://192.168.1.10:8080/api/data", body)
    if (res["status"] == 201) {
        console.log("data sent")
    }
}

Pattern: Custom Headers

package main

import "http"
import "crypto"

fn main():void {
    var token: string = "Bearer " + crypto.sha256("my-secret")
    var headers: dict = {"Authorization": token}
    var res: dict = http.request("GET", "http://api.example.com/secure", "", headers, 5000)
    console.log(res["status"])
}

URL Format

http://host/path
http://host:port/path
Component Default
Port 80 (http), 443 (https)
Path /

DNS hostnames are resolved via getaddrinfo.

Notes

  • HTTP/1.1 with Connection: close — one connection per request
  • POST/PUT default Content-Type: application/json
  • Custom Content-Type can be set via http.request() headers
  • Status code 0 means connection failed (DNS error, refused, timeout)
  • 10 second default timeout for connect/send/recv
  • Response body is read until connection closes (max 1MB)
  • No HTTPS yet — TLS support planned for future version
  • On ESP32, uses lwIP socket layer