Search results

There are no results.

std.multipart

Parsing and generating of multipart/form-data streams, based on RFC 7578.

multipart/form-data streams are typically used for HTTP forms that contain file fields, as application/x-www-form-urlencoded forms don't allow for efficient streaming of data to a server.

Examples

Parsing streams is done using the Multipart type, capable of parsing any stream that implements std.io.Read:

import std.io (Buffer)
import std.multipart (Multipart)

let input = '--BOUNDARY\r
Content-Disposition: form-data; name="name"\r
content-type: text/plain\r
\r
This is the value.\r
--BOUNDARY--'

let parser = Multipart.new(Buffer.new(input), boundary: 'BOUNDARY')
let field = parser.next.get.get
let buf = ByteArray.new

field.name                   # => 'name'
field.content_type           # => 'text/plain'
field.read_all(buf).or_panic # => 18
buf.to_string                # => 'This is the value.'

Generating a multipart stream is done using Form and FormField:

import std.io (Buffer)
import std.multipart (Form)
import std.stdio (Stdout)

let form = Form.new(Stdout.new, boundary: 'TEST')

form.field('name').text('Alice') # => Result.Ok(nil)
form.field('message').read(Buffer.new('hello')) # => Result.Ok(nil)
form.close.or_panic # => Result.Ok(nil)

Methods

boundary_separator

Returns a randomly generated boundary separator.

Types

Error

An error produced when parsing a multipart stream.

Field

A field in a multipart/form-data stream.

Form

A type for constructing a multipart/form-data stream of fields and their values.

FormField

A type for constructing a field in a multipart stream.

Multipart

A type for iterating over multipart/form-data fields and their values.