title: Streaming data in Ruby net/http PUT request
body: <p>In the <a href="https://ruby-doc.org/stdlib-2.6.1/libdoc/net/http/rdoc/Net/HTTP.html" rel="nofollow noreferrer">Ruby-doc Net/HTTP</a> there is a detailed example for streaming response bodies - it applies when you try to download a large file.</p>
  
  <p>I am looking for an equivalent code snippet to upload a file via PUT. Spent quite a bit of time trying to make code work with no luck. I think I need to implement a particular interface and pass it the to <code>request.body_stream</code></p>
  
  <p>I need <strong>streaming</strong> because I want to alter the content of the file while it is being uploaded so I want to have access to the buffered chunks while the upload takes place. I would gladly use a library like http.rb or rest-client as long as I can use streaming. </p>
  
  <p>Thanks in advance!
  For reference following is the working non streamed version</p>
  
  <pre><code>
      uri = URI("http://localhost:1234/upload")
  
      Net::HTTP.start(uri.host, uri.port) do |http|
        request = Net::HTTP::Put.new uri
  
        File.open("/home/files/somefile") do |f|
          request.body = f.read()
        end
  
        # Ideally I would need to use **request.body_stream** instead of **body** to get streaming
  
        http.request request do |response|
          response.read_body do |result|
            # display the operation result   
            puts result
          end
        end
      end
  </code></pre>