简体   繁体   中英

Ruby unable to send SSL request but Curl Can

I got the error in Ruby. But it works in CURL .

Environment is rbenv

Ruby version : ruby 2.1.2p95 (2014-05-08 revision 45877) [x86_64-darwin13.0]

error: SSL_connect returned=1 errno=0 state=SSLv3 read server certificate B: certificate verify failed

Curl

curl -k https://xx:443/ -i \
-H "Accept: application/json" \
-H "Content-Type:application/json" \
-X POST -d @${1} https://xx:443/service/

Ruby

@uri = URI.parse(server_url)
@https = Net::HTTP.new(@uri.host, @uri.port)
@https.use_ssl = true

req = Net::HTTP::Post.new(@uri.path, initheader = @header )
to_send_body = {
  "ServiceID" => "SignUp",
  "UserID" => "fs.test@dsd.com",
  "Password" => "123456789"
}
req.body = to_send_body.to_json
res = @https.request(req)

data.json

{"ServiceID" : "SignUp","UserID" : "poc.test@123.com","Password" : "123456789"}

UPDATE

I finished it by curb gem finally, but still failed on http way,

curb version

c = Curl::Easy.http_post("https://1xx.xx.x.2:443/service/", to_send_body.to_json   
    ) do |curl|
      curl.ssl_verify_peer = false
      curl.headers['Accept'] = 'application/json'
      curl.headers['Content-Type'] = 'application/json'
    end        

The -k flag in curl means that it will still connect when the cert is invalid. To accomplish the same thing in ruby, use the following

@https = Net::HTTP.new(@uri.host, @uri.port)
@https.use_ssl = true
@https.verify_mode = OpenSSL::SSL::VERIFY_NONE

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM