0

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" => "[email protected]",
  "Password" => "123456789"
}
req.body = to_send_body.to_json
res = @https.request(req)

data.json

{"ServiceID" : "SignUp","UserID" : "[email protected]","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        

1 Answer 1

0

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
Sign up to request clarification or add additional context in comments.

2 Comments

How could I send the json file content in Ruby? In Curl, I only need to specify the json file path. thanks
try req.body = to_send_body.to_json

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.