dream_http_client/stream
Streaming HTTP request functionality
This module provides streaming HTTP request functionality that returns a yielder of response chunks as they arrive. Use this for:
- Large file downloads
- Streaming AI responses
- Real-time data feeds
- Any case where you want to process data incrementally
Quick Start
import dream_http_client/client
import dream_http_client/stream
import gleam/yielder
client.new
|> client.host("cdn.example.com")
|> client.path("/large-file.zip")
|> stream.stream_request()
|> yielder.each(fn(chunk) {
// Process each chunk as it arrives
save_chunk(chunk)
})
Processing Streams
The yielder can be consumed with yielder.each(), yielder.to_list(), or
other yielder functions. Each chunk is a BytesTree that you can convert
to a string or process as binary data.
Values
pub fn stream_request(
req: client.ClientRequest,
) -> yielder.Yielder(bytes_tree.BytesTree)
Create a yielder that streams HTTP response chunks
Sends an HTTP request and returns a yielder that produces chunks of the response body as they arrive from the server. This allows you to process large responses incrementally without loading the entire response into memory.
The yielder produces Next(chunk, new_state) for each chunk until the stream
completes, then produces Done.
Parameters
req: The configured HTTP request
Returns
A Yielder that produces BytesTree chunks. Use yielder.each() to process
chunks, or yielder.to_list() to collect all chunks.
Example
import dream_http_client/client
import dream_http_client/stream
import gleam/yielder
import gleam/bytes_tree
// Stream and process chunks
client.new
|> client.host("api.example.com")
|> client.path("/stream")
|> stream.stream_request()
|> yielder.each(fn(chunk) {
let body = bytes_tree.to_string(chunk)
process_chunk(body)
})
// Or collect all chunks
let chunks = client.new
|> client.host("cdn.example.com")
|> client.path("/file.zip")
|> stream.stream_request()
|> yielder.to_list()