Stores form data to be used in a POST
or PUT
request.
The form data given in params
consists of zero or more fields; each field is:
A scalar value.
A name/value pair.
An IO
stream opened for reading.
Argument params
should be an Enumerable (method params.map
will be called), and is often an array or hash.
First, we set up a request:
_uri = uri.dup _uri.path ='/posts' req = Net::HTTP::Post.new(_uri)
Argument params
As an Array
When params
is an array, each of its elements is a subarray that defines a field; the subarray may contain:
One string:
req.set_form([['foo'], ['bar'], ['baz']])
Two strings:
req.set_form([%w[foo 0], %w[bar 1], %w[baz 2]])
When argument enctype
(see below) is given as 'multipart/form-data'
:
A string name and an IO
stream opened for reading:
require 'stringio' req.set_form([['file', StringIO.new('Ruby is cool.')]])
A string name, an IO
stream opened for reading, and an options hash, which may contain these entries:
:filename
: The name of the file to use.
:content_type
: The content type of the uploaded file.
Example:
req.set_form([['file', file, {filename: "other-filename.foo"}]]
The various forms may be mixed:
req.set_form(['foo', %w[bar 1], ['file', file]])
Argument params
As a Hash
When params
is a hash, each of its entries is a name/value pair that defines a field:
The name is a string.
The value may be:
nil
.
Another string.
An IO
stream opened for reading (only when argument enctype
– see below – is given as 'multipart/form-data'
).
Examples:
# Nil-valued fields. req.set_form({'foo' => nil, 'bar' => nil, 'baz' => nil}) # String-valued fields. req.set_form({'foo' => 0, 'bar' => 1, 'baz' => 2}) # IO-valued field. require 'stringio' req.set_form({'file' => StringIO.new('Ruby is cool.')}) # Mixture of fields. req.set_form({'foo' => nil, 'bar' => 1, 'file' => file})
Optional argument enctype
specifies the value to be given to field 'Content-Type'
, and must be one of:
'application/x-www-form-urlencoded'
(the default).
'multipart/form-data'
; see RFC 7578.
Optional argument formopt
is a hash of options (applicable only when argument enctype
is 'multipart/form-data'
) that may include the following entries:
:boundary
: The value is the boundary string for the multipart message. If not given, the boundary is a random string. See Boundary.
:charset
: Value is the character set for the form submission. Field names and values of non-file fields should be encoded with this charset.
returns “type/subtype” which is MIME Content-Type. It is downcased for canonicalization. Content-Type parameters are stripped.
Returns a list of encodings in Content-Encoding field as an array of strings.
The encodings are downcased for canonicalization.
Generate a random URL-safe base64 string.
The argument n specifies the length, in bytes, of the random number to be generated. The length of the result string is about 4/3 of n.
If n is not specified or is nil, 16 is assumed. It may be larger in the future.
The boolean argument padding specifies the padding. If it is false or nil, padding is not generated. Otherwise padding is generated. By default, padding is not generated because “=” may be used as a URL delimiter.
The result may contain A-Z, a-z, 0-9, “-” and “_”. “=” is also used if padding is true.
require 'random/formatter' Random.urlsafe_base64 #=> "b4GOKm4pOYU_-BOXcrUGDg" # or prng = Random.new prng.urlsafe_base64 #=> "UZLdOkzop70Ddx-IJR0ABg" prng.urlsafe_base64(nil, true) #=> "i0XQ-7gglIsHGV2_BNPrdQ==" prng.urlsafe_base64(nil, true) #=> "-M8rLhr7JEpJlqFGUMmOxg=="
See RFC 3548 for the definition of URL-safe base64.
Generates formatted random number from raw random bytes. See Random#rand
.
Creates an unsigned certificate for subject
and key
. The lifetime of the key is from the current time to age
which defaults to one year.
The extensions
restrict the key to the indicated uses.
Use new_ui
for the duration of block
.
Discard statistics collected for --yjit-stats
.
Return a hash for statistics generated for the --yjit-stats
command line option. Return nil
when option is not passed or unavailable. If a symbol argument is provided, return only the value for the named stat. If any other type is provided, raises TypeError
.
Foo::Bar ^^^^^^^^
foo => [, bar, ] ^^^^^^^^^^^
def foo(…); bar(…); end ^^^