title: Ruby script to send image with net/http and POST request
body: <p>On a ruby project I am trying to send a image to a server listening to POSTS requests. I would like to avoid using extra necessary gems. I am using ruby 1.9.3 and net/http
  Multiport is not supported by the server and the following code:</p>
  
  <pre><code>require 'net/http'  
  require "uri"
  
  image = File.open(image.path, 'r')
  
  url = URI("http://someserver/#{image_name}")
  req = Net::HTTP.new(url.host, url.port)
  Net::HTTP.new(url.host, url.port)
  headers = {"X-ClientId" =&gt; client_id, "X-ClientSecret" =&gt; client_secret, "Content-Type" =&gt; "image/jpeg"}
  response, body = req.post(url.path, image, headers)
  </code></pre>
  
  <p>crashes with the following error message:</p>
  
  <pre><code>http.rb:1932:in `send_request_with_body': undefined method `bytesize' for #&lt;File:0x007ffdcd335270&gt; (NoMethodError)
  </code></pre>
  
  <p>While the following succeed in bash:</p>
  
  <pre><code>curl -i -X POST --data-binary @./output.jpg -H 'X-ClientId: ##..##' -H 'X-ClientSecret: ##..##' http:/someserver/output.jpg
  </code></pre>
  
  <p>Any idea what is going wrong?</p>
  
  <p>Thanks in advance</p>