Search results

There are no results.

std.multipart.Form

type pub Form[T: mut + Write[E], E]

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

A Form wraps a Write type and writes its fields directly to the underlying stream.

Closing a Form

A multipart form/stream is closed using a dedicated boundary delimiter. This delimiter is produced using Form.close. While this method is called when a Form is dropped, it's recommended to call this method explicitly as this allows for handling of any errors that may be produced when writing the closing boundary delimiter.

Static methods

new

Show source code
Hide source code
fn pub static new(writer: T, boundary: String) -> Self {
  Self(
    writer: writer,
    boundary: boundary,
    buffer: ByteArray.with_capacity(FIELD_COPY_SIZE),
    open: true,
  )
}
fn pub static new(writer: T: mut, boundary: String) -> Form[T: mut, E]

Returns a new Form that writes to writer using the boundary string boundary (minus the leading --).

Instance methods

close

Show source code
Hide source code
fn pub move close -> Result[Nil, E] {
  try write_close
  @open = false
  Result.Ok(nil)
}
fn pub move close -> Result[Nil, E]

Writes the final boundary terminator, consuming self in the process.

If this method is called explicitly, dropping a Form won't write the closing delimiter a second time.

Errors

This method returns an error if the closing delimiter can't be written to the underlying stream (e.g. it's a socket and the connection is closed).

Examples

import std.multipart (Form, FormField)
import std.stdio (Stdout)

let form = Form.new(Stdout.new, 'BOUNDARY')

form.field('name').text('Alice') # => Result.Ok(nil)
form.close # => Result.Ok(nil)

field

Show source code
Hide source code
fn pub mut field(name: String) -> FormField[T, E] {
  FormField(
    form: self,
    name: name,
    file_name: '',
    content_type: '',
    content_range: '',
  )
}
fn pub mut field(name: String) -> FormField[T: mut, E]

Returns a new FormField that will write its data to self.

Examples

import std.multipart (Form, FormField)
import std.stdio (Stdout)

let form = Form.new(Stdout.new, 'BOUNDARY')

form.field('name')

Implemented traits

std.drop.

Drop

impl Drop for Form