Sets the value for field 'Range'
; see Range request header:
With argument length
:
req = Net::HTTP::Get.new(uri) req.set_range(100) # => 100 req['Range'] # => "bytes=0-99"
With arguments offset
and length
:
req.set_range(100, 100) # => 100...200 req['Range'] # => "bytes=100-199"
With argument range
:
req.set_range(100..199) # => 100..199 req['Range'] # => "bytes=100-199"
Net::HTTPHeader#range=
is an alias for Net::HTTPHeader#set_range
.
Returns the value of field 'Content-Length'
as an integer, or nil
if there is no such field; see Content-Length request header:
res = Net::HTTP.get_response(hostname, '/nosuch/1') res.content_length # => 2 res = Net::HTTP.get_response(hostname, '/todos/1') res.content_length # => nil
Sets the value of field 'Content-Length'
to the given numeric; see Content-Length response header:
_uri = uri.dup hostname = _uri.hostname # => "jsonplaceholder.typicode.com" _uri.path = '/posts' # => "/posts" req = Net::HTTP::Post.new(_uri) # => #<Net::HTTP::Post POST> req.body = '{"title": "foo","body": "bar","userId": 1}' req.content_length = req.body.size # => 42 req.content_type = 'application/json' res = Net::HTTP.start(hostname) do |http| http.request(req) end # => #<Net::HTTPCreated 201 Created readbody=true>
Returns a Range
object representing the value of field 'Content-Range'
, or nil
if no such field exists; see Content-Range response header:
res = Net::HTTP.get_response(hostname, '/todos/1') res['Content-Range'] # => nil res['Content-Range'] = 'bytes 0-499/1000' res['Content-Range'] # => "bytes 0-499/1000" res.content_range # => 0..499
Returns the media type from the value of field 'Content-Type'
, or nil
if no such field exists; see Content-Type response header:
res = Net::HTTP.get_response(hostname, '/todos/1') res['content-type'] # => "application/json; charset=utf-8" res.content_type # => "application/json"
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.
Create a new ArgumentsNode
node.
Create a new ArrayNode
node.
Create a new CaseNode
node.
Create a new ElseNode
node.
Create a new FalseNode
node.
Create a new ImaginaryNode
node.
Create a new IntegerNode
node.
Create a new SelfNode
node.
Create a new SuperNode
node.
Create a new UntilNode
node.
Retrieve the value of one of the LoopFlags
flags.
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
.
Return a Hash
for RJIT statistics. --rjit-stats makes more information available.
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.