DEV Community

Code Salley
Code Salley

Posted on

Basic Auth with Ruby on Rails(Faraday gem)

Faraday gem comes in handy with several plugins(middleware) that make HTTP request easier and more customisable, Basic authentication in ruby turn to be tricky. let's digest it;

require "faraday"

request_helper = Faraday.new(url: 'example.com') do |builder|
                builder.use Faraday::Request::BasicAuthentication, client_key, secret_key
             end

# you make HTTP requests using `request_helper` since basic auth is configured

response = request_helper.get('/myendpoit')
Enter fullscreen mode Exit fullscreen mode

Using an API like PAYPAL API, you need to get tokens by proving client and secret keys. With Faraday::Request::BasicAuthentication middleware we can pass client and secret keys as arguments to achieve basic authentication using the same method above.

twitter: code salley

Discussion (0)