Sergey Makridenkov
Sergey Makridenkov

Reputation: 624

Closed stream (IO::Error) with HTTP::Client.get("https...")

I am a bit confused with two codes.

    uri = URI.parse("https://api.exchangeratesapi.io")
    client = HTTP::Client.new(uri)
    client.get("/latest?base=EUR") do |response|
      p response.body_io.gets
    end

It works as expected, but

    response = HTTP::Client.get("https://api.exchangeratesapi.io/latest?base=EUR")
    p response

failed with

Closed stream (IO::Error)
         from /usr/share/crystal/src/io.cr:128:5 in 'check_open'
         from /usr/share/crystal/src/io/buffered.cr:99:5 in 'peek'
         from /usr/share/crystal/src/io.cr:632:37 in 'gets'
         from /usr/share/crystal/src/io.cr:591:5 in 'gets'
         from /usr/share/crystal/src/io.cr:814:5 in 'read_line:chomp'
         from /usr/share/crystal/src/http/content.cr:205:7 in 'read_chunk_size'
         from /usr/share/crystal/src/http/content.cr:187:26 in 'next_chunk'
         from /usr/share/crystal/src/http/content.cr:120:7 in 'read'
         from /usr/share/crystal/src/io.cr:554:29 in 'gets_to_end'
         from /usr/share/crystal/src/http/client/response.cr:81:15 in 'consume_body_io'
         from lib/webmock/src/webmock/core_ext.cr:12:7 in 'exec_internal'
         from /usr/share/crystal/src/http/client.cr:576:5 in 'exec'
         from /usr/share/crystal/src/http/client.cr:698:5 in 'exec'
         from /usr/share/crystal/src/http/client.cr:730:7 in 'exec'
         from /usr/share/crystal/src/http/client.cr:402:3 in ‚get

I use Crystal 0.33. Any idea what could fix second code?

Upvotes: 1

Views: 212

Answers (1)

Oleh Prypin
Oleh Prypin

Reputation: 34116

In the log I see lib/webmock/src/webmock/core_ext.cr (which normally shouldn't be there), seems like its influence is causing the problem, and the code in isolation would just work.

Upvotes: 1

Related Questions