Maintenance Ruby 3.4 gets fixes, but no new features. Switch to Ruby 4.0 →
instance method
Net::HTTPHeader#content_length=
(::Integer len) -> void
content_length=(len)
lib/net/http/header.rb
View on GitHub ↗
# File tmp/rubies/ruby-3.4.1/lib/net/http/header.rb, line 637
def content_length=(len)
unless len
@header.delete 'content-length'
return nil
end
@header['content-length'] = [len.to_i.to_s]
end
Sets the value of field 'Content-Length' to the given numeric; see Content-Length response header:
Example
_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>
See also
#content_length
#content_range
#connection_keep_alive?
#content_type
also in
4.1 · 4.0 · 3.3 · 3.2 · 3.1 · 3.0