Skip to content

Instantly share code, notes, and snippets.

@danilogco
Forked from mislav/gist:938183
Last active April 24, 2021 19:47
Show Gist options
  • Save danilogco/1adbcdaffa4400672436cabd83fa16fb to your computer and use it in GitHub Desktop.
Save danilogco/1adbcdaffa4400672436cabd83fa16fb to your computer and use it in GitHub Desktop.
Faraday SSL example
connection = Faraday::Connection.new('http://example.com') do |builder|
builder.request :url_encoded # for POST/PUT params
builder.adapter :net_http
end
# same as above, short form:
connection = Faraday.new 'http://example.com'
# GET
connection.get '/posts'
# POST payload
payload = {:title => 'Example'}
connection.post '/posts', payload
# POST file
conn = Faraday.new(url: URL) do |faraday|
faraday.request :multipart #make sure this is set before url_encoded
faraday.request :url_encoded
faraday.adapter Faraday.default_adapter
end
file = Faraday::UploadIO.new(
params[:image].tempfile.path,
params[:image].content_type,
params[:image].original_filename
)
payload = { :file => file }
conn.post('/', payload)
# now again, over SSL
# verify_mode is automatically set to OpenSSL::SSL::VERIFY_PEER
connection = Faraday.new 'https://example.com'
# turn off SSL
# (no use-case for this, really)
connection = Faraday.new 'https://example.com', :ssl => false
# turn off peer verification
connection = Faraday.new 'https://example.com', :ssl => {:verify => false}
# other SSL options
connection = Faraday.new 'https://example.com', :ssl => {
:client_cert => ...,
:client_key => ...,
:ca_file => ...,
:ca_path => ...,
:cert_store => ...
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment