HTTP::Multipart

module HTTP::Multipart

Overview

The HTTP::Multipart module contains utilities for parsing MIME multipart messages, which contain multiple body parts, each containing a header section and binary body. The multipart/form-data content-type has a separate set of utilities in the HTTP::FormData module.

Defined in:

http/multipart/builder.cr
http/multipart/parser.cr
http/multipart.cr

Class Method Summary

Class Method Detail

def self.build(boundary = Multipart.generate_boundary, &block)Source

Yields a Multipart::Builder to the given block, returning the generated message as a String.

def self.generate_boundarySource

Returns a unique string suitable for use as a multipart boundary.

HTTP::Multipart.generate_boundary # => "---------------------------dQu6bXHYb4m5zrRC3xPTGwV"

def self.parse(io, boundary, &block)Source

Parses a MIME multipart message, yielding HTTP::Headers and an IO for each body part.

Please note that the IO object yielded to the block is only valid while the block is executing. The IO is closed as soon as the supplied block returns.

multipart = "--aA40\r\nContent-Type: text/plain\r\n\r\nbody\r\n--aA40--"
HTTP::Multipart.parse(IO::Memory.new(multipart), "aA40") do |headers, io|
  headers["Content-Type"] # => "text/plain"
  io.gets_to_end          # => "body"
end

See: Multipart::Parser

def self.parse(request : HTTP::Request, &block)Source

Parses a MIME multipart message, yielding HTTP::Headers and an IO for each body part.

Please note that the IO object yielded to the block is only valid while the block is executing. The IO is closed as soon as the supplied block returns.

headers = HTTP::Headers{"Content-Type" => "multipart/mixed; boundary=aA40"}
body = "--aA40\r\nContent-Type: text/plain\r\n\r\nbody\r\n--aA40--"
request = HTTP::Request.new("POST", "/", headers, body)

HTTP::Multipart.parse(request) do |headers, io|
  headers["Content-Type"] # => "text/plain"
  io.gets_to_end          # => "body"
end

See: Multipart::Parser

def self.parse_boundary(content_type)Source

Extracts the multipart boundary from the Content-Type header. May return nil is the boundary was not found.

HTTP::Multipart.parse_boundary("multipart/mixed; boundary=\"abcde\"") # => "abcde"

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

在线笔记
App下载
App下载

扫描二维码

下载编程狮App

公众号
微信公众号

编程狮公众号

意见反馈
返回顶部