2
votes

I am getting OpenSSL::SSL::SSLError while making one http_request. Please find the code mentioned below.

 require 'net/http'
 uri = URI.parse("http://webaddress.com")
 http = Net::HTTP.new(uri.host, uri.port)
 http.use_ssl = true
 http.verify_mode = OpenSSL::SSL::VERIFY_NONE
 request = Net::HTTP::Post.new("/v1.1/auth")
 request.add_field('Content-Type', 'application/json')
 request.body = {"auth_token"=>"abcd"  ,"employee" => {"method" => "add_employee"}}
 response = http.request(request)

It's throwing following error :

SSL_connect returned=1 errno=0 state=SSLv2/v3 read server hello A: unknown protocol

Please let me know why this error is coming? and what could be the reason for this error.

1
http.use_ssl = true it should be false as you call httpRajarshi Das
@RajarshiDas it's working. but its throwing another error undefined method `bytesize' for #<Hash:0xb4335ec8>Can Can
should I create a post ?Rajarshi Das
request.body = {"auth_token"=>"abcd" ,"employee" => {"method" => "add_employee"} for thisRajarshi Das
ya sure. please do thatCan Can

1 Answers

2
votes

http.use_ssl = true it should be false as you call http

It should be

http.use_ssl = false

secondly

You're trying to pass a Hash, {"auth_token"=>"abcd" ,"employee" => {"method" => "add_employee"}}, to post where it expects to see a String. I think you want something more like this:

query = {"auth_token"=>"abcd"  ,"employee" => {"method" => "add_employee"}}.to_query

it will create string like this "auth_token=abcd&employee%5Bmethod%5D=add_employee" you have to send this to post

http.post(uri.path, query, headers)

Please try this

 require 'net/http'
 uri = URI.parse("http://webaddress.com/v1.1/auth")
 http = Net::HTTP.new(uri.host, uri.port)
 http.use_ssl = false
 http.verify_mode = OpenSSL::SSL::VERIFY_NONE

 response = http.post(uri.path,{"auth_token"=>"abcd", "employee" => {"method" => "add_employee"}}.to_query)

Look at the below links for more http://ruby-doc.org/stdlib-1.9.3/libdoc/net/http/rdoc/Net/HTTP.html#method-i-post

[33] pry(main)> uri = URI.parse("http://webaddress.com/v1.1/auth") 
=> #<URI::HTTP:0x000000098f9928 URL:http://webaddress.com/v1.1/auth>
[34] pry(main)> http = Net::HTTP.new(uri.host, uri.port) 
=> #<Net::HTTP webaddress.com:80 open=false>
[35] pry(main)> http.use_ssl = false 
=> false
[36] pry(main)> http.verify_mode = OpenSSL::SSL::VERIFY_NONE 
=> 0

[38] pry(main)> response = http.post(uri.path,{"auth_token"=>"abcd", "employee" =>    {"method" => "add_employee"}}.to_query) 
=> #<Net::HTTPFound 302 Found readbody=true>
[39] pry(main)> response.body
=> ""