title: Download an image from a URL?
body: <p>I am trying to use HTTP::get to download an image of a Google chart from a URL I created. </p>
  
  <p>This was my first attempt:</p>
  
  <pre><code>failures_url  = [title, type, data, size, colors, labels].join("&amp;")
  
  require 'net/http'
  
  Net::HTTP.start("http://chart.googleapis.com") { |http|
    resp = http.get("/chart?#{failures_url")
    open("pie.png" ,"wb") { |file|
      file.write(resp.body)
    }
  }
  </code></pre>
  
  <p>Which produced only an empty PNG file.</p>
  
  <p>For my second attempt I used the value stored inside <code>failure_url</code> inside the <code>http.get()</code> call.</p>
  
  <pre><code>require 'net/http'
  
  Net::HTTP.start("http://chart.googleapis.com") { |http|
    resp = http.get("/chart?chtt=Builds+in+the+last+12+months&amp;cht=bvg&amp;chd=t:296,1058,1217,1615,1200,611,2055,1663,1746,1950,2044,2781,1553&amp;chs=800x375&amp;chco=4466AA&amp;chxl=0:|Jul-2010|Aug-2010|Sep-2010|Oct-2010|Nov-2010|Dec-2010|Jan-2011|Feb-2011|Mar-2011|Apr-2011|May-2011|Jun-2011|Jul-2011|2:|Months|3:|Builds&amp;chxt=x,y,x,y&amp;chg=0,6.6666666666666666666666666666667,5,5,0,0&amp;chxp=3,50|2,50&amp;chbh=23,5,30&amp;chxr=1,0,3000&amp;chds=0,3000")
    open("pie.png" ,"wb") { |file|
      file.write(resp.body)
    }
  }
  </code></pre>
  
  <p>And, for some reason, this version works even though the first attempt had the same data inside the <code>http.get()</code> call. Does anyone know why this is?</p>
  
  <p>SOLUTION:</p>
  
  <p>After trying to figure why this is happening I found "<a href="https://stackoverflow.com/questions/2263540/how-do-i-download-a-binary-file-over-http-using-ruby">How do I download a binary file over HTTP?</a>".</p>
  
  <p>One of the comments mentions removing <code>http://</code> in the <code>Net::HTTP.start(...)</code> call otherwise it won't succeed. Sure enough after I did this:</p>
  
  <pre><code>failures_url  = [title, type, data, size, colors, labels].join("&amp;")
  
  require 'net/http'
  
  Net::HTTP.start("chart.googleapis.com") { |http|
    resp = http.get("/chart?#{failures_url")
    open("pie.png" ,"wb") { |file|
      file.write(resp.body)
    }
  }
  </code></pre>
  
  <p>it worked.</p>