Convert Figma logo to code with AI

lostisland logofaraday

Simple, but flexible HTTP client library, with support for multiple backends.

5,723
972
5,723
41

Top Related Projects

Simple HTTP and REST client for Ruby, inspired by microframework syntax for specifying actions.

:tada: Makes http fun again!

Quick Overview

Faraday is a popular HTTP client library for Ruby. It provides a simple and flexible interface for making HTTP requests, with support for various adapters, middleware, and advanced features like automatic retries and request/response logging.

Pros

  • Modular architecture with swappable adapters and middleware
  • Extensive ecosystem with many plugins and extensions
  • Consistent API across different HTTP backends
  • Excellent test coverage and active maintenance

Cons

  • Learning curve for advanced features and customizations
  • Performance overhead compared to bare metal HTTP libraries
  • Some features require additional gems or dependencies
  • Documentation can be overwhelming for beginners

Code Examples

Basic GET request:

require 'faraday'

response = Faraday.get('https://api.example.com/users')
puts response.body

POST request with JSON payload:

conn = Faraday.new(url: 'https://api.example.com') do |f|
  f.request :json
  f.response :json
end

response = conn.post('/users') do |req|
  req.body = { name: 'John Doe', email: 'john@example.com' }
end

puts response.body['id']

Custom middleware and error handling:

conn = Faraday.new(url: 'https://api.example.com') do |f|
  f.request :retry, max: 3, interval: 0.5
  f.response :logger
  f.use MyCustomMiddleware
end

begin
  response = conn.get('/users')
rescue Faraday::ClientError => e
  puts "Request failed: #{e.message}"
end

Getting Started

  1. Install the gem:
gem install faraday
  1. Create a basic connection:
require 'faraday'

conn = Faraday.new(url: 'https://api.example.com')

response = conn.get('/users')
puts response.status
puts response.body
  1. Add middleware for JSON handling:
conn = Faraday.new(url: 'https://api.example.com') do |f|
  f.request :json
  f.response :json
end

response = conn.post('/users', { name: 'Alice', email: 'alice@example.com' })
puts response.body['id']

Competitor Comparisons

Simple HTTP and REST client for Ruby, inspired by microframework syntax for specifying actions.

Pros of rest-client

  • Simpler API with a more intuitive interface for basic HTTP requests
  • Built-in support for cookies and redirection
  • Easier to get started with for beginners

Cons of rest-client

  • Less flexible for advanced use cases
  • Fewer middleware options and extensibility features
  • Not as actively maintained as Faraday

Code Comparison

rest-client:

require 'rest-client'
response = RestClient.get('https://api.example.com/users')
puts response.body

Faraday:

require 'faraday'
conn = Faraday.new(url: 'https://api.example.com')
response = conn.get('/users')
puts response.body

Both Faraday and rest-client are popular Ruby gems for making HTTP requests. Faraday offers more flexibility and extensibility, making it suitable for complex applications and advanced use cases. It provides a modular architecture with middleware support, allowing developers to customize request/response handling.

rest-client, on the other hand, offers a simpler and more straightforward API, making it easier for beginners to get started. It includes built-in support for cookies and automatic redirection handling.

While rest-client is easier to use for basic requests, Faraday's flexibility makes it a better choice for larger projects or those requiring more advanced features. Faraday also benefits from more active maintenance and a larger community.

:tada: Makes http fun again!

Pros of HTTParty

  • Simpler and more lightweight, making it easier to get started quickly
  • Built-in support for parsing JSON and XML responses
  • Intuitive DSL for defining HTTP methods and options

Cons of HTTParty

  • Less flexible for complex HTTP scenarios
  • Limited middleware support compared to Faraday
  • Fewer built-in features for advanced use cases (e.g., retry logic, connection pooling)

Code Comparison

HTTParty:

class GitHub
  include HTTParty
  base_uri 'https://api.github.com'
  
  def repos(user)
    self.class.get("/users/#{user}/repos")
  end
end

Faraday:

conn = Faraday.new(url: 'https://api.github.com') do |faraday|
  faraday.request  :url_encoded
  faraday.response :json
  faraday.adapter  Faraday.default_adapter
end

response = conn.get("/users/#{user}/repos")

Both HTTParty and Faraday are popular Ruby HTTP client libraries, but they cater to different needs. HTTParty is more straightforward and suitable for simpler API integrations, while Faraday offers greater flexibility and extensibility for complex HTTP scenarios.

Convert Figma logo designs to code with AI

Visual Copilot

Introducing Visual Copilot: A new AI model to turn Figma designs to high quality code using your components.

Try Visual Copilot

README

Faraday

Gem Version GitHub Actions CI GitHub Discussions

Faraday is an HTTP client library abstraction layer that provides a common interface over many adapters (such as Net::HTTP) and embraces the concept of Rack middleware when processing the request/response cycle. Take a look at Awesome Faraday for a list of available adapters and middleware.

Why use Faraday?

Faraday gives you the power of Rack middleware for manipulating HTTP requests and responses, making it easier to build sophisticated API clients or web service libraries that abstract away the details of how HTTP requests are made.

Faraday comes with a lot of features out of the box, such as:

  • Support for multiple adapters (Net::HTTP, Typhoeus, Patron, Excon, HTTPClient, and more)
  • Persistent connections (keep-alive)
  • Parallel requests
  • Automatic response parsing (JSON, XML, YAML)
  • Customization of the request/response cycle with middleware
  • Support for streaming responses
  • Support for uploading files
  • And much more!

Getting Started

The best starting point is the Faraday Website, with its introduction and explanation.

Need more details? See the Faraday API Documentation to see how it works internally, or take a look at Advanced techniques for calling HTTP APIs in Ruby blog post from @mattbrictson 🚀

Supported Ruby versions

This library aims to support and is tested against the currently officially supported Ruby implementations. This means that, even without a major release, we could add or drop support for Ruby versions, following their EOL. Currently that means we support Ruby 3.0+

If something doesn't work on one of these Ruby versions, it's a bug.

This library may inadvertently work (or seem to work) on other Ruby implementations and versions, however support will only be provided for the versions listed above.

If you would like this library to support another Ruby version, you may volunteer to be a maintainer. Being a maintainer entails making sure all tests run and pass on that implementation. When something breaks on your implementation, you will be responsible for providing patches in a timely fashion. If critical issues for a particular implementation exist at the time of a major release, support for that Ruby version may be dropped.

Contribute

Do you want to contribute to Faraday? Open the issues page and check for the help wanted label! But before you start coding, please read our Contributing Guide

Copyright

© 2009 - 2023, the Faraday Team. Website and branding design by Elena Lo Piccolo.