mamantoha / crest

HTTP and REST client for Crystal
https://mamantoha.github.io/crest/
MIT License
235 stars 14 forks source link

Return expression from response block #129

Closed ProjectMoon closed 4 years ago

ProjectMoon commented 5 years ago

Feature request: When doing something like this:

Crest.get("http://example.com") do |resp|
    resp.body_io
end

Would it be possible to have the method return the expression from the block? break/next don't seem to be respected here, and after looking at the source I think it's because this version of the method always just returns nil?

Or am I missing something?

mamantoha commented 5 years ago

This is for streaming. With a block, an Crest::Response body is returned and the response's body is available as an IO.

You can use

response = ""

Crest.get("http://httpbin.org/get") do |resp|
  response = resp.body_io.gets_to_end
end

which is equal to

response = Crest.get("http://httpbin.org/get").body
ProjectMoon commented 5 years ago

In the use case I have, I want to stream XML in and then convert that whole body to a hash. Didn't realize I could just assign it to a variable outside the block.