@eryx/http/multipart Module

JSON

Summary

Functions

multipart.encodeFields(fields: { MultipartField })(string, string)
multipart.parse(body: string, contentTypeOrBoundary: string){ ParsedMultipartField }

API Reference

Functions

multipart.encodeFields

Encodes a list of form fields as a multipart/form-data body.

Returns the encoded body string and the Content-Type header (which includes the boundary). Pass both to a request function:

local body, contentType = http.multipart({
    { name = "username", value = "alice" },
    { name = "avatar", value = fileContents, filename = "photo.png", contentType = "image/png" },
})

local resp = http.post("http://example.com/upload", body, {
    headers = { ["Content-Type"] = contentType },
})
multipart.encodeFields(fields: { MultipartField })(string, string)

Parameters

fields: { MultipartField }

A list of MultipartField entries.

Returns

string

The encoded multipart body.

string

The Content-Type header value including boundary.

multipart.parse

Parses a multipart/form-data body into individual fields.

Pass the raw body and either the full Content-Type header value or just the boundary string.

multipart.parse(body: string, contentTypeOrBoundary: string){ ParsedMultipartField }

Types

MultipartField

A single field in a multipart form.

For file uploads, provide filename and optionally contentType. The value is the raw file content as a string.

name: string

The form field name.

value: string

The field value (text or file content).

filename: string?

Optional filename for file upload fields.

contentType: string?

Optional MIME type. Defaults to application/octet-stream for files.

ParsedMultipartField

name: string
value: string
filename: string?
contentType: string?
headers: { [string]: string }