e x p r e s s i c a

ruby on rails, business and technicalities

No public Twitter messages.

  • RSS
  • Facebook
  • Twitter
  • Linkedin

If you encounter this issue while making a Net::HTTP post, most likely it’s the issue when you are trying with an https url.

The problem occurs when the Net:HTTP object is not explicitly set to use SSL and still using the secured url.

Usually, for posting a form with Net:HTTP for http url we do it like this:

url = URI.parse('http://www.domain.com/path/to/be/posted')
req = Net::HTTP::Post.new(url.path)
req.set_form_data({'name'=>'Sur Max', 'email'=>'some@email.com'})
http = Net::HTTP.new(url.host, url.port)
response = http.request(req)

For posting to https urls, explicitly mention to use ssl:

url = URI.parse('https://www.domain.com/path/to/be/posted')
req = Net::HTTP::Post.new(url.path)
req.set_form_data({'name'=>'Sur Max', 'email'=>'some@email.com'})
http = Net::HTTP.new(url.host, url.port)
http.use_ssl = true
response = http.request(req)

And when domain is coming as a parameter or via a variable, do it like this:

url = URI.parse(domain)
req = Net::HTTP::Post.new(url.path)
req.set_form_data({'name'=>'Sur Max', 'email'=>'some@email.com'})
http = Net::HTTP.new(url.host, url.port)
http.use_ssl = true if domain =~ /^https/
response = http.request(req)

6 Responses so far.

  1. Anand says:

    Thank you, this post was extremely useful!

  2. Nick says:

    I ran into the same problem. I have seen some others solve this by actually changing the NET::HTTP code. There seems to be an incorrect check at the socket layer, that is closing the socket too soon, which causes the EOF error.

  3. Sur says:

    @Nick

    I agree with you that there is a bug in the Net::HTTP code.

    Logically, if we are supplying “https..” url to Net::HTTP, it should automatically set the flag use_ssl = true on it’s own… and it’s not doing that. Thus we have to manually set the flag to use_ssl = true.

    This is a workaround and not a fix to the problem. But since use_ssl= has been explicitly provided has a method to be used and set the flag, I didn’t mind adding it as part of code, and all my jobs are working with no issue the day I’ve added this flag.

    Did your code work after this or you are still facing issue ?

    • Nick says:

      I was able to get it working thanks. However, I cheated by using a new networking library (httpi -> httpclient). Of course, I did run into a bug using that library, which I was able to work around (http://www.ruby-forum.com/topic/1490195).

      Unfortunately, it seems that while Ruby is excellent in networking overall, it has many buggy implementations/methods when using SSL.

      Nick

  4. Sam says:

    Thanks a lot! The net/http library is really user-unfriendly to make you have to explicitly specify use_ssl instead of just looking at the protocol. And of going ahead with making a request to a URL with https protocol without you having set the right property to true. Well who knows? Maybe it’s gotten better in 1.9.

    • Sur says:

      I am glad if you’re able to use the above fix and get things going.

      Maybe yes… else anyone of us can submit a patch for the same.


Sponsors