Skip to content

Maintenance Ruby 3.3 gets fixes, but no new features. Switch to Ruby 4.0 →

instance method

Net::HTTPResponse#body_encoding=

body_encoding=(value)

Sets the encoding that should be used when reading the body:

  • If the given value is an Encoding object, 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
See also #body= #inspect #body #read_body also in 3.4 · 3.2 · 3.1