Ruby 2.5.0 リファレンスマニュアル > ライブラリ一覧 > net/httpライブラリ > Net::HTTPGenericRequestクラス > body_stream

instance method Net::HTTPGenericRequest#body_stream

body_stream -> object[permalink][rdoc]
body_stream=(f)

サーバに送るリクエストのエンティティボディを IO オブジェクトなどのストリームで設定します。 f は read(size) メソッドが定義されている必要があります。

[PARAM] f:
エンティティボディのデータを得るストリームオブジェクトを与えます。


require 'net/http'

uri = URI.parse('http://www.example.com/index.html')
post = Net::HTTP::Post.new(uri.request_uri)
File.open("/path/to/test", 'rb') do |f|
  # 大きなファイルを扱う際にメモリ消費を少なくできる
  post.body_stream = f
  post["Content-Length"] = f.size
end
post.body_stream # => #<File:/path/to/test (closed)>