[RUBY] [Rails] Try using Faraday middleware

I tried using Faraday middleware, so I will summarize it easily.

What is Faraday?

HTTP client library written in ruby You can use this to access external APIs from your Rails project.

What is Faraday middleware?

Faraday has middleware that allows you to customize request and response processing. There are two types, Request middleware and Response middleware. By using this, you can add authentication information at the time of request and write out the communication in the log.

You can use it as follows using symbols.


Faraday.new(url: 'https://test.com') do |builder|
  #Use middleware
  builder.request :url_encoded
  builder.response :logger, Config.logger if Config.enable_logger

Request middleware

Request middleware can set the details of the request before sending the request. Here, we will introduce two middleware. Authentication middleware can set the Authorization header, UrlEncoded middleware can convert the hash of key / value pairs into a URL-encoded request body.

Example of use


#Authentication middleware
builder.request :basic_auth, "username", "password"
builder.request : authorization :Basic, "aut_key"

#UrlEncoded middleware
builder.request :url_encoded

Response middleware

The response middleware can set the details of the response when it receives the response. Here, we will introduce Logger middleware. Logger middleware allows you to log request and response body and headers. It also has a filter function that allows you to filter sensitive information using regular expressions.

Example of use


#Logger middleware
builder.response :logger
builder.response :logger do | logger |
  logger.filter(/(password=)(\w+)/, '\1[REMOVED]')

Even more convenient with faraday_middleware!

If you add a gem called faraday_middleware, you can use more middleware.

For example, you can use the ParseJson class and specify parser_options and content_type to parse the hash key as a symbol for JSON.

Example of use


builder.response :json, parser_options: { symbolize_names: true }, content_type: 'application/json'

Reference document

https://lostisland.github.io/faraday/ https://lostisland.github.io/faraday/middleware/list

Recommended Posts

[Rails] Try using Faraday middleware
Try using view_component with rails
Try using libGDX
Try using powermock-mockito2-2.0.2
Try using GraalVM
Try using jmockit 1.48
Try using sql-migrate
Try using SwiftLint
Try using Log4j 2.0
Try using Axon Framework
Try using JobScheduler's REST-API
Try using java.lang.Math methods
Try using PowerMock's WhiteBox
Try using Talend Part 2
Try using F # list
Try using each_with_index method
Try using Spring JDBC
Try to implement tagging function using rails and js
Try using the query attribute of Ruby on Rails
Try using RocksDB in Java
Search function using [rails] ransack
Try using GloVe with Deeplearning4j
SNS authentication using Rails google
[Rails] Save images using carrierwave
Try using Cocoa from Ruby
Try using letter_opener_web for inquiries
Japaneseize using i18n with Rails
[Rails] Japanese localization using rails-i18n
[Rails] Test code using Rspec
Organize Rails routing using draw
Try using IntelliJ IDEA once
Ajax bookmark function using Rails
Error when using rails capybara
Try using Spring Boot Security
Try using gRPC in Ruby
[Processing] Try using GT Force.
Detailed tips when using Rails
[Programming Encyclopedia] ยง2 Try using Ruby
[Rails 6] Star-shaped review using Raty.js
People using docker Try using docker-compose
[Hidden_field] Let's send information using rails hidden_field !!!!
[Ruby on Rails] Read try (: [],: key)
Get PV (views) using Impressionist ~ Rails
[Rails] Tag management function (using acts-as-taggable-on)
Try using Redmine on Mac docker
[Rails 6] API development using GraphQL (Query)
Try using Redis with Java (jar)
[Java] Try to implement using generics
Try using the messaging system Pulsar
[Rails 6] destroy using the resources method
Try using IBM Java method tracing
[Rails] Status update using Rake task
[Rails] Reflection to db using seeds.rb
Try using Hyperledger Iroha's Java SDK
[Java] Where did you try using java?