简体   繁体   中英

How can I force close excon connection when using chunked request

I am trying to read the first chunk of each image I am requesting to get its mime type and size which I'm able to do.

However, when I use Connection#reset it doesn't kill the connection and keeps downloading next chunks.

I am just wondering is it possible to close the connection after getting the first chunk?

This is my code right now

streamer = lambda do |chunk, _remaining_bytes, total_bytes|
  image_format = MimeMagic.by_magic(chunk)
  # other code
  @connection.reset
end

Excon.defaults[:chunk_size] = 25
@connection = Excon.new(image_url)
@connection.get(response_block: streamer)

I don't believe there is a way currently to stop before the chunked response concludes. That being said, it might be possible that you could get the data you want from a head request and avoid the need for a get request?

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM