2

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)

Community
  • 1
  • 1
Shadwell
  • 34,314
  • 14
  • 94
  • 99
  • 1
    In general the chunk boundaries could end up splitting multibyte characters. Writing out the data as is seems the right thing to do to me. – Frederick Cheung Apr 30 '15 at 12:31
  • Yeah, I was a bit nervous about chunking across the middle of a character. I'd hoped that setting the encoding would prevent that too. – Shadwell Apr 30 '15 at 12:48
  • Encoding is important if you're treating the data as text. It's meaningless if you're treating the data as binary. – the Tin Man Apr 30 '15 at 18:34

0 Answers0