简体   繁体   中英

Ruby/Rails client to HTTPS API

I need to deal with some third-party API, which is require https connection. I even have a certificate file mycert.pem and try to connect with API this way:

https = Net::HTTP.new(uri.host, uri.port)
https.use_ssl = true
https.verify_mode = OpenSSL::SSL::VERIFY_PEER
https.ca_file = File.join(Rails.root, "data", "certs", "mycert.pem")
https.ca_path = [Rails.root, "data", "certs"].join("/")

https.start {
  data = {
      <here some request data>
  }.to_json

  https.request_post([API_HOST, API_URL].join('/'), data) {|res|
    print res.body
  }
}

But I always have authorization error form this API, just like I have invalid (or obsolete) certificate. Even I'm sure that this is not true and my client problem, cause other client connects with this API (and same certificate) with no problem.

So, question is

what is the right way to https connect with service from ruby? Maybe I've lost something or do something wrong?

I don't have the answer for that specific problem but you can try a lot of libraries for doing this that may help you around.

Check out this awesome slideshow comparison:

http://www.slideshare.net/HiroshiNakamura/rubyhttp-clients-comparison

I'm using faraday for my projects.

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