Skip to content

class MIME::Multipart::Parser
inherits Reference

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.

Class methods

.new(io : IO, boundary : String)

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

View source

Methods

#has_next?

True if #next can be called legally.

View source

#next

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
View source