Maintenance Ruby 3.3 gets fixes, but no new features. Switch to Ruby 4.0 →
instance method
Net::HTTPResponse#body_encoding=
body_encoding=(value)
lib/net/http/response.rb
View on GitHub ↗
# File tmp/rubies/ruby-3.3.0/lib/net/http/response.rb, line 253
def body_encoding=(value)
value = Encoding.find(value) if value.is_a?(String)
@body_encoding = value
end
Sets the encoding that should be used when reading the body:
-
If the given value is an
Encodingobject, that encoding will be used. -
Otherwise if the value is a string, the value of Encoding#find(value) will be used.
-
Otherwise an encoding will be deduced from the body itself.
Examples:
Example
http = Net::HTTP.new(hostname) req = Net::HTTP::Get.new('/') http.request(req) do |res| p res.body.encoding # => #<Encoding:ASCII-8BIT> end http.request(req) do |res| res.body_encoding = "UTF-8" p res.body.encoding # => #<Encoding:UTF-8> end