DEV Community

Code Salley
Code Salley

Posted on

1

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

Top comments (0)

A Workflow Copilot. Tailored to You.

Pieces.app image

Our desktop app, with its intelligent copilot, streamlines coding by generating snippets, extracting code from screenshots, and accelerating problem-solving.

Read the docs

👋 Kindness is contagious

Please leave a ❤️ or a friendly comment on this post if you found it helpful!

Okay