Modifying Headers in Faraday

Posted by lawrend on September 16, 2017

I searched for way too long to find this information so if you need this, I hope it helps.

Faraday can be used to get a url:

  Faraday.get ''

And that works fine but usually we want to add params. Most documentation looks something like this:

conn =
  response = conn.get ''
conn.params  = {'tesla' => 'coil'}
conn.headers = {'Accept' => 'vnd.github-v3+json'}

Great! instantiate an instance of Faraday and then set params and headers. But what if we are setting up our Faraday like this?

resp = Faraday.get('') do |req|


I found plenty of info about setting params, but it took me a long time to find info on setting headers. But here is what it looks like:

resp = Faraday.get('') do |req|

  req.params['client_id'] = 1234
	req.headers['Accept'] = 'application/json'

It is obvious once I found it but it was frustrating bit of time. I was specifically trying to modify the response from github when going through the authorization process using Oauth. The authorization token is returned in a string and I wanted it returned in JSON. Changing the Accept header was all I had to do but I couldn’t figure out how to do it :)

Hope this helps someone else!