title: Downloading image from url in Ruby and Rails
body: <p>I am trying to download an image from url in ruby and rails, so far what I have done is </p>
  
  <pre><code>require 'base64'
          parsedUri = URI.parse('http://somewebsite.com/path/to/image.jpeg')
          http = Net::HTTP.new(parsedUri.host, parsedUri.port)
          if params[:src].include? "https"
              http.use_ssl = true 
          end
          request = Net::HTTP::Get.new(parsedUri.request_uri, {'User-Agent' =&gt; 'Mozilla/5.0 (X11; Ubuntu; Linux x86_64; rv:31.0) Gecko/20100101 Firefox/31.0'})
          response = http.request(request)
          image = Base64.encode64(response.body)
          File.open('some.jpeg', "w+") do |f|
            f.write(image)
          end
  </code></pre>
  
  <p>,
  Now this code does make a file in my root directory but when I open it, it given me error "Error interpreting JPEG image file (Not a JPEG file: starts with 0x64 0x61)" the irony is when I open that file in gedit and use the same base64 encoded to render image in browser using img tag. The image gets rendered. I am using Ubuntu. Please help it is driving me nuts</p>