HTTP::FormData::Builder

class HTTP::FormData::Builder

Overview

Builds a multipart/form-data message.

Example

io = IO::Memory.new
builder = HTTP::FormData::Builder.new(io, "aA47")
builder.field("name", "joe")
file = IO::Memory.new "file contents"
builder.file("upload", io, FileMetadata.new(filename: "test.txt"))
builder.finish
io.to_s # => "--aA47\r\nContent-Disposition: form-data; name=\"name\"\r\n\r\njoe\r\n--aA47\r\nContent-Disposition: form-data; name=\"upload\"; filename=\"test.txt\"\r\n\r\nfile contents\r\n--aA47--"

Defined in:

http/formdata/builder.cr

Class Method Summary

Instance Method Summary

Class Method Detail

def self.new(io : IO, boundary = Multipart.generate_boundary)Source

Creates a new FormData::Builder which writes to io, using the multipart boundary boundary-

Instance Method Detail

def boundary : StringSource

def content_typeSource

Returns a content type header with correct boundary parameter.

builder = HTTP::FormData::Builder.new(io, "a4VF")
builder.content_type # => "multipart/form-data; boundary=\"a4VF\""

def field(name, value, headers : HTTP::Headers = HTTP::Headers.new)Source

Adds a form part with the given name and value. Headers can optionally be provided for the form part.

def file(name, io, metadata : FileMetadata = FileMetadata.new, headers : HTTP::Headers = HTTP::Headers.new)Source

Adds a form part called name, with data from io as the value. Metadata can be provided to add extra metadata about the file to the Content-Disposition header for the form part. Other headers can be added using headers.

def finishSource

Finalizes the multipart message, this method must be called before the generated multipart message written to the IO is considered valid.

© 2012–2017 Manas Technology Solutions.
Licensed under the Apache License, Version 2.0.
https://crystal-lang.org/api/0.22.0/HTTP/FormData/Builder.html

在线笔记
App下载
App下载

扫描二维码

下载编程狮App

公众号
微信公众号

编程狮公众号

意见反馈
返回顶部