Giter VIP home page Giter VIP logo

Comments (5)

strobemonkey avatar strobemonkey commented on June 10, 2024 1

I had the same error so I broke down the code and stuck in a trace, similar to this:

url = URI.parse('http://www.imageshack.us/upload_api.php')
file = 'c:/x.jpg'
io = UploadIO.new(jpg, "image/jpeg", 'c:/x.jpg')
req = Net::HTTP::Post::Multipart.new url.path, io
http = Net::HTTP.new(url.host, url.port)
http.set_debug_output $stderr if true  # dump out stuff
res = http.start { |http| http.request(req) }
puts res

My problem occurred because I was posting via https - http worked fine - which I resolved by adding this line to the above code, before the 'http.start' line:

require 'net/https'
http.use_ssl = url.port == 443

Hope this helps.

from multipart-post.

dje29 avatar dje29 commented on June 10, 2024

This helped ;)
If someone has a better solution I'm open for proposal, but here is my final implementation.
It handles redirection and save the file into a temp file that is returned (unclosed)

   require 'uri'
   require 'net/http'
   require 'net/https'
   def http_fetch( uri_str, temp_file_name, limit = 10 )
     # You should choose better exception.
     raise ArgumentError, 'HTTP redirect too deep' if limit == 0

     url = URI.parse( uri_str )

     req = Net::HTTP::Get.new url.path

     http = Net::HTTP.new(url.host, url.port)
     if url.scheme == "https"
       http.use_ssl = url.port == 443
     end

     #http.set_debug_output $stderr if true  # dump out stuff
     begin
       response = http.start { |http| http.request(req) }
       case response
       when Net::HTTPRedirection 
       then
         http_fetch(response['location'], temp_file_name, limit - 1 )

       when Net::HTTPSuccess
       then
         file = ::Tempfile.new( temp_file_name )
         file.binmode
         file.unlink
         file.write( response.body )
         file.rewind
         file
       else
         response.error!
       end
     end
   rescue EOFError
     # Reached the end of the response
   end

from multipart-post.

ioquatix avatar ioquatix commented on June 10, 2024

Can you please confirm if this is still an issue?

from multipart-post.

sanjusoftware avatar sanjusoftware commented on June 10, 2024

I tried all the above methods to no avail ... does anyone know what the real problem is ... looks like in this context does not have anything to do with net/http(s) because I see this error on following line:

file = 'iron_man.jpg'
io = UploadIO.new(file, 'image/jpeg')

do we know why is that happening?

from multipart-post.

ioquatix avatar ioquatix commented on June 10, 2024

What error are you seeing?

from multipart-post.

Related Issues (20)

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.