Skip to content

class HTTP::FormData::Parser
inherits Reference

Class methods

.new(io, boundary)

Creates a new 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 form-data part and yields field name, io, FileMetadata, and raw headers.

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 "http"

form_data = "--aA40\r\nContent-Disposition: form-data; name=\"field1\"; filename=\"foo.txt\"; size=13\r\nContent-Type: text/plain\r\n\r\nfield data\r\n--aA40--"
parser = HTTP::FormData::Parser.new(IO::Memory.new(form_data), "aA40")
parser.next do |part|
  part.name                    # => "field1"
  part.body.gets_to_end        # => "field data"
  part.filename                # => "foo.txt"
  part.size                    # => 13
  part.headers["Content-Type"] # => "text/plain"
end
View source