Is it possible to force an encoding on a Net::HTTP
response read using read_body
? I know that it is possible to call force_encoding
on the response.body
itself but that means you have to read the whole response in and loses the benefit of reading it in pieces.
I'm reading a csv file from a uri (I don't want to parse the CSV just yet) and writing it to a file locally:
Net::HTTP.start(uri.host, uri.port) do |http|
request = Net::HTTP::Get.new(uri)
http.request request do |response|
File.open(target_file, 'w') do |f|
response.read_body do |chunk|
f.write(chunk)
end
end
end
end
The content is encoded as ISO-8859-1 but Net::HTTP
treats it as ASCII 8BIT.
(I've worked round the problem by writing it out as binary and dealing with the encoding later)