Skip to content

Instantly share code, notes, and snippets.

@mlsaito
Last active July 21, 2023 08:09
Show Gist options
  • Save mlsaito/1b149f7fd4e070c40465913f8c89a2c5 to your computer and use it in GitHub Desktop.
Save mlsaito/1b149f7fd4e070c40465913f8c89a2c5 to your computer and use it in GitHub Desktop.
Specify SSL version (TLS 1.2) on HTTParty - a Ruby client library

Implementation

Some vendors require you to call their API via TLS 1.2 and I don't see that much documentation on how to do this.

2 ways to make an HTTP request using HTTParty:

  1. Directly calling Class method.
# Notice the `ssl_version` option, to specify ssl version of the HTTP request
response = HTTParty.get('http://api.stackexchange.com/2.2/questions?site=stackoverflow', ssl_version: :TLSv1_2)
  1. Wrap HTTParty in your own class.
class StackExchange
  include HTTParty
  base_uri 'api.stackexchange.com'
  # HTTParty would inherit this option
  ssl_version :TLSv1_2

  def initialize(service, page)
    @options = { query: { site: service, page: page } }
  end

  def questions
    self.class.get("/2.2/questions", @options)
  end

  def users
    self.class.get("/2.2/users", @options)
  end
end

stack_exchange = StackExchange.new("stackoverflow", 1)
puts stack_exchange.questions
puts stack_exchange.users

Testing

Paypal made a dedicated endpoint mainly for this purpose, since they're one of the vendors who requires such ssl version. Reference: https://www.paypal.com/au/webapps/mpp/tls-http-upgrade

Confirm if TLS 1.2 is used properly and received by the server.

curl https://tlstest.paypal.com

Expected Output:

PayPal_Connection_OK

You could also do a pry on your staging / production server and executing HTTParty.get directly. If you encounter an error, you must upgrade the OPENSSL library on your machine.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment