开发者

Using Faraday Ruby gem to download image and write to disk

开发者 https://www.devze.com 2023-04-10 01:55 出处:网络
When using the Faraday gem to hit a URL for an image like this: http_conn = Faraday.new do |builder| builder.adapter Faraday.default_adapter

When using the Faraday gem to hit a URL for an image like this:

http_conn = Faraday.new do |builder|
  builder.adapter Faraday.default_adapter
end 
response = ht开发者_如何学JAVAtp_conn.get 'http://example.tld/image.png'

How can you write the image file from the Faraday response to disk?

Debugging the response.body attribute shows it is binary data.

Any help greatly appreciated.


Why not just write the response.body like any other file?

File.open('image.png', 'wb') { |fp| fp.write(response.body) }
0

精彩评论

暂无评论...
验证码 换一张
取 消

关注公众号