Skip to content

Commit

Permalink
Set Content-Type header to multipart/form-data when uploading a file
Browse files Browse the repository at this point in the history
  • Loading branch information
sferik committed Aug 26, 2012
1 parent d4318f4 commit 24f759b
Show file tree
Hide file tree
Showing 2 changed files with 7 additions and 1 deletion.
7 changes: 7 additions & 0 deletions lib/twitter/request/multipart_with_file.rb
Original file line number Diff line number Diff line change
Expand Up @@ -3,14 +3,21 @@
module Twitter
module Request
class MultipartWithFile < Faraday::Middleware
CONTENT_TYPE = 'Content-Type'.freeze
class << self
attr_accessor :mime_type
end
self.mime_type = 'multipart/form-data'.freeze

def call(env)
if env[:body].is_a?(Hash)
env[:body].each do |key, value|
if value.is_a?(File)
env[:body][key] = Faraday::UploadIO.new(value, mime_type(value.path), value.path)
env[:request_headers][CONTENT_TYPE] = self.class.mime_type
elsif value.is_a?(Hash) && (value[:io].is_a?(IO) || value[:io].is_a?(StringIO))
env[:body][key] = Faraday::UploadIO.new(value[:io], mime_type('.' + value[:type]), '')
env[:request_headers][CONTENT_TYPE] = self.class.mime_type
end
end
end
Expand Down
1 change: 0 additions & 1 deletion spec/helper.rb
Original file line number Diff line number Diff line change
@@ -1,4 +1,3 @@
# encoding: utf-8
unless ENV['CI']
require 'simplecov'
SimpleCov.start do
Expand Down

0 comments on commit 24f759b

Please sign in to comment.