Skip to content

Releases

6.0.0

Choose a tag to compare

@github-actions github-actions released this 25 Nov 01:01
92918cd

dream_http_client 6.0.0 Release Notes

Release Date: 2026-11-25

This release moves dream_http_client from Erlang's httpc to gun. Requests to one host now share HTTP/2 connections, and the number of streams on a connection follows what the server allows. Errors are typed values, a response body is bytes, and a stream can send typed messages to an OTP actor.

It is a major version because four public types changed, StreamMessage.DecodeError is removed, and HttpResponse.body is a BitArray. Read the Migration Guide before you upgrade. This release was first prepared as 5.2.0 and never published, so it follows 5.1.3. The package also moved to its own repository.

Key Highlights

  • gun is the HTTP backend: HTTP/1.1 and HTTP/2, with connections that requests to one host share
  • Typed errors (breaking): TransportError and StreamFailure take the place of error strings
  • Response bodies are bytes (breaking): HttpResponse.body is a BitArray. Read text with client.body_text(response)
  • stream_to: a stream sends typed messages to a Subject, for use in an OTP actor
  • A pool of your own: client.new_pool(max_connections:) gives a group of requests its own connections
  • Per-request settings: protocols (with h2c), connect_timeout, auto_redirect and certificate_authority_file
  • Global transport configuration: TransportConfig with 11 gun settings and log_level
  • Its own repository: dream_http_client now lives at TrustBound/dream_http_client, with its own version and its own releases

What's New

The HTTP Backend Is gun

The HTTP client backend has been replaced from Erlang's httpc to
gun. This brings:

  • HTTP/2 support: native multiplexing over a single TCP connection
    when connecting to HTTP/2-capable servers
  • Connection pooling: managed by a new dream_http_conn_manager
    gen_server with per-host multi-connection pools. An HTTP/1.1 connection
    carries one request at a time, an HTTP/2 connection carries as many
    streams as the server allows, and automatic dead-connection cleanup
    removes connections that stopped
  • Stale connection retry: a request that hits a server-closed
    connection is sent once more on a fresh connection when its method is
    idempotent, or when the server did not process it
  • Protocol negotiation: TLS connections negotiate HTTP/2 via ALPN,
    falling back to HTTP/1.1; plain TCP uses HTTP/1.1

send(), stream_yielder(), and start_stream() keep their behaviour, apart
from the error type changes in "Structured Error Types" below.

Structured Error Types

All string-based error representations have been replaced with structured Gleam
types that preserve the full detail gun provides.

Motivation

String errors ("econnrefused", "timeout", "HTTP 500 Internal Server Error: ...")
hide production issues. Consumers cannot reliably pattern-match on error categories
for retry logic, alerting, or circuit breakers without fragile string parsing.

New types

TransportError has 9 variants. Eight preserve all gun error details:
StreamReset(code, description), Goaway(code, description),
ConnectionError(code, description), RemoteClosed, TimedOut(timeout_ms),
ProcessDown(reason), ConnectFailed(reason) and Unexpected(raw). The
ninth, NoRecording(method, url, directory, nearest), is a playback
request that no recording matches, which the client does not send. It names
the nearest recordings and the parts that differ.

The code of StreamReset, Goaway and ConnectionError is an
Http2ErrorCode: one constructor for each HTTP/2 error code of RFC 9113,
for example RefusedStream, and UnknownErrorCode(name) for any other code.

Explain a transport error Tested snippet
import dream_http_client/client.{
  type TransportError, ConnectFailed, Goaway, NoError, RefusedStream,
  StreamReset, TimedOut, transport_error_to_string,
}
import gleam/int

pub fn explain(error: TransportError) -> String {
  case error {
    ConnectFailed(reason: reason) -> "Cannot reach server: " <> reason
    TimedOut(timeout_ms: ms) -> "Timed out after " <> int.to_string(ms) <> "ms"
    StreamReset(code: RefusedStream, ..) -> "The server refused the request"
    Goaway(code: NoError, ..) -> "The server is going away"
    _ -> transport_error_to_string(error)
  }
}

StreamFailure distinguishes HTTP failures from transport errors:
HttpFailure(response: HttpResponse) and TransportFailure(error: TransportError).

Explain a stream failure Tested snippet
import dream_http_client/client.{
  type StreamFailure, ConnectFailed, HttpFailure, TransportFailure,
  transport_error_to_string,
}
import gleam/int

pub fn explain_failure(failure: StreamFailure) -> String {
  case failure {
    HttpFailure(response: response) -> "HTTP " <> int.to_string(response.status)
    TransportFailure(error: ConnectFailed(reason: reason)) ->
      "Cannot connect: " <> reason
    TransportFailure(error: error) -> transport_error_to_string(error)
  }
}

Typed Stream Messages With stream_to

stream_to(request, inbox) starts a stream that sends typed StreamMessage
values to a Subject(StreamMessage): StreamStart, a Chunk for each part of
the body, then exactly one StreamEnd or StreamError. It returns the
RequestId that each message carries. Use it in an OTP actor: add the inbox to
the actor selector with process.select. The stream stops when the process
that called stream_to() stops. Record and playback work as with
start_stream().

Stream a download into an actor Tested snippet
fn start_download(
  done: Subject(Result(BitArray, String)),
) -> Result(actor.Initialised(State, StreamMessage, Nil), String) {
  let inbox = process.new_subject()
  let request =
    client.new()
    |> scheme(http.Http)
    |> host("localhost")
    |> port(9876)
    |> path("/stream/fast")
  // The actor owns the stream: the stream stops when the actor stops
  use _request_id <- result.map(stream_to(request, inbox))
  let selector = process.new_selector() |> process.select(inbox)
  actor.initialised(State(body: <<>>, done: done))
  |> actor.selecting(selector)
}

fn handle_stream_message(
  state: State,
  message: StreamMessage,
) -> actor.Next(State, StreamMessage) {
  case message {
    StreamStart(..) -> actor.continue(state)
    Chunk(data: data, ..) ->
      actor.continue(State(..state, body: bit_array.append(state.body, data)))
    StreamEnd(..) -> {
      process.send(state.done, Ok(state.body))
      actor.stop()
    }
    StreamError(error: error, ..) -> {
      process.send(state.done, Error(stream_failure_to_string(error)))
      actor.stop()
    }
  }
}

cancel_stream(request_id) works with the RequestId that stream_to()
returns. It stops the stream process, cancels the request, and returns after
the stream has stopped, so no message of the stream arrives after it returns. A
cancel of a stream that has ended, or a second cancel of the same stream, does
nothing and returns Nil.

Cancel a stream Tested snippet
pub fn start_then_cancel() -> Result(Nil, String) {
  let inbox = process.new_subject()
  let request =
    client.new()
    |> client.scheme(http.Http)
    |> client.host("localhost")
    |> client.port(9876)
    |> client.path("/stream/fast")
  use request_id <- result.map(client.stream_to(request, inbox))
  // Later: no message of this stream comes after the cancel
  client.cancel_stream(request_id)
}

Response Bodies Are Bytes

HttpResponse.body is a BitArray: the exact bytes the server sent. Before,
it was a String, and a body that was not UTF-8 (an image, a PDF, text in
another encoding) was a String that was not valid text. On streams, the
body of an error response was read as Latin-1, and its bytes were lost.

client.body_text(response) returns Ok(text) for a UTF-8 body and
Error(Nil) for any other body.

Read the body as text Tested snippet

import dream_http_client/client

pub fn text_of(response: client.HttpResponse) -> String {
  case client.body_text(response) {
    Ok(text) -> text
    Error(Nil) -> "The body is not text"
  }
}

A request body is bytes too. client.body(String) sets a text body, as
before. client.body_bits(BitArray) sets a body that is not text.

Upload a file Tested snippet</...

Read more