atomvm_gleam/http

Types

Request body for request.

pub type Body {
  Empty
  Bytes(BitArray)
  Stream
}

Constructors

  • Empty

    No body (undefined / nil upstream).

  • Bytes(BitArray)

    Inline body; Content-Length is set automatically.

  • Stream

    Streamed upload via stream_request_body. Pair with an explicit Content-Length header.

Opaque HTTP connection handle.

pub type Connection

Errors from ahttp_client.

pub type Error {
  Failed
  NotSupported
  Badarg
  Timeout
  Other(String)
}

Constructors

  • Failed
  • NotSupported
  • Badarg
  • Timeout
  • Other(String)

http or https.

pub type Protocol {
  Http
  Https
}

Constructors

  • Http
  • Https

Opaque request reference from request.

pub type Ref

One parsed response element from recv / stream.

pub type Response {
  Status(ref: Ref, code: Int)
  Header(ref: Ref, name: BitArray, value: BitArray)
  HeaderContinuation(ref: Ref, name: BitArray, value: BitArray)
  TrailerHeader(ref: Ref, name: BitArray, value: BitArray)
  Data(ref: Ref, body: BitArray)
  Done(ref: Ref)
}

Constructors

  • Status(ref: Ref, code: Int)
  • Header(ref: Ref, name: BitArray, value: BitArray)
  • HeaderContinuation(ref: Ref, name: BitArray, value: BitArray)

    Deprecated: AtomVM 0.7 no longer emits obs-fold / header continuation. Kept for binary compatibility with older response lists.

  • TrailerHeader(ref: Ref, name: BitArray, value: BitArray)

    Trailer field after a chunked body (0.7).

  • Data(ref: Ref, body: BitArray)
  • Done(ref: Ref)

Outcome of feeding one mailbox message into stream.

pub type StreamEvent {
  Responses(Connection, List(Response))
  Closed(Connection)
  Unknown
}

Constructors

  • Responses(Connection, List(Response))

    Parsed HTTP response pieces for this message.

  • Closed(Connection)

    Peer closed after a complete response (or with no in-flight parse).

  • Unknown

    Message was not a socket message for this connection.

TLS / socket verify mode passed through to ssl options.

pub type Verify {
  VerifyNone
  VerifyPeer
}

Constructors

  • VerifyNone
  • VerifyPeer

Values

pub fn close(conn: Connection) -> Result(Nil, Error)

Close the connection.

See ahttp_client:close/1.

pub fn connect(
  protocol: Protocol,
  host: String,
  port: Int,
  active: Bool,
  verify: option.Option(Verify),
) -> Result(Connection, Error)

Connect to an HTTP(S) server.

See ahttp_client:connect/4.

pub fn error_to_string(error: Error) -> String

Format an Error for logging.

pub fn recv(
  conn: Connection,
  len: Int,
) -> Result(#(Connection, List(Response)), Error)

Receive and parse up to len bytes (0 = all pending).

See ahttp_client:recv/2.

pub fn request(
  conn: Connection,
  method: String,
  path: String,
  headers: List(#(String, String)),
  body: Body,
) -> Result(#(Connection, Ref), Error)

Send an HTTP request.

Pass Empty for no body, Bytes for an inline payload, or Stream to upload with stream_request_body.

See ahttp_client:request/5.

pub fn stream(
  conn: Connection,
  message: message,
) -> Result(StreamEvent, Error)

Feed a socket mailbox message into the HTTP parser (active mode).

Returns updated connection + response events, Closed on a clean peer close, or Unknown when message is unrelated. Parser failures (line too long, incomplete response, invalid chunk size, …) map to Error / Other.

See ahttp_client:stream/2.

pub fn stream_request_body(
  conn: Connection,
  ref: Ref,
  chunk: BitArray,
) -> Result(#(Connection, Ref), Error)

Upload one chunk of a streamed request body.

Only valid after request with Stream. ref must be the reference from that request.

See ahttp_client:stream_request_body/3.

✨ Search Document