3

I have inherited a Ruby project forked from CASinoApp, which depends on CASino, which uses Faraday for HTTP requests. I really do not want to fork CASino to modify how it invokes Faraday.post.

Is there a way to configure Faraday globally to set it's User-Agent for any-and-all requests that follow? Specifically, I cannot run Faraday's constructor to get a connection and configure it - because CASino would not then use my configured connection for it's requests. I need to somehow mutate Faraday's internal "default" configuration.

Chris Trahey
  • 18,202
  • 1
  • 42
  • 55
  • Just found this, will explore tomorrow and follow up here if it works: https://github.com/lostisland/faraday/blob/5ba7c30a7bb1d755c6e777b16b68ba1c498a49a7/lib/faraday.rb#L123 – Chris Trahey Aug 10 '17 at 20:49

3 Answers3

5

In modern versions of Faraday, you should be able to use Faraday.default_connection_options. In our case, we were stuck using 0.9.2 and had to actually configure a connection object and set Faraday.default_connection for reasons addressed in 0.12.2.

Faraday.default_connection = Faraday.new(options = {:headers=>{:user_agent=>"My Custom UA Here"}})
Chris Trahey
  • 18,202
  • 1
  • 42
  • 55
3

For anyone specifically trying to set default timeout options like we were, this worked for us in a modern Faraday implementation:

Faraday.default_connection_options = Faraday::ConnectionOptions.new({timeout: 5, open_timeout: 5})

You could also set any other request options inside that hash.

BryanP
  • 680
  • 6
  • 26
1

This is what worked for me in the faraday version 1.1.0.

Faraday.default_connection_options.headers = { "User-Agent" => 'Mozilla/5.0' }
Rodrigo Dias
  • 1,331
  • 2
  • 13
  • 18