W3cubDocs

/Crystal

class MIME::Multipart::Parser

Overview

Parses multipart MIME messages.

Example

require "mime/multipart"

multipart = "--aA40\r\nContent-Type: text/plain\r\n\r\nbody\r\n--aA40--"
parser = MIME::Multipart::Parser.new(IO::Memory.new(multipart), "aA40")

while parser.has_next?
  parser.next do |headers, io|
    headers["Content-Type"] # => "text/plain"
    io.gets_to_end          # => "body"
  end
end

Please note that the IO object yielded by #next is only valid until the block returns.

Defined in:

mime/multipart/parser.cr

Constructors

Instance Method Summary

Constructor Detail

def self.new(io : IO, boundary : String)Source

Creates a new Multipart::Parser which parses io with multipart boundary boundary.

Instance Method Detail

def has_next?Source

True if #next can be called legally.

def next(&)Source

Parses the next body part and yields headers as HTTP::Headers and the body text as an IO.

This method yields once instead of returning the values, because the IO object yielded to the block is only valid while the block is executing. The IO object will be closed as soon as the block returns. To store the content of the body part for longer than the block, the IO must be read into memory.

require "mime/multipart"

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

© 2012–2020 Manas Technology Solutions.
Licensed under the Apache License, Version 2.0.
https://crystal-lang.org/api/0.35.1/MIME/Multipart/Parser.html