Upload files using Faraday

It turns out that I needed to specify an adapter. Here's the code that ended up working.

conn = Faraday.new('http://myapi') do |f|
  f.request :multipart
  f.request :url_encoded
  f.adapter :net_http # This is what ended up making it work
end

payload = { :file => Faraday::UploadIO.new('...', 'image/jpeg') }

conn.post('/', payload)

Something like this resolved my problem:

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)

Tags:

Ruby

Faraday