Browse files

Set Content-Type header to multipart/form-data when uploading a file

  • Loading branch information...
1 parent d4318f4 commit 24f759b7a128de2bceff27ee0e4699e8d927e5a5 @sferik committed Aug 26, 2012
Showing with 7 additions and 1 deletion.
  1. +7 −0 lib/twitter/request/multipart_with_file.rb
  2. +0 −1 spec/helper.rb
View
7 lib/twitter/request/multipart_with_file.rb
@@ -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
View
1 spec/helper.rb
@@ -1,4 +1,3 @@
-# encoding: utf-8
unless ENV['CI']
require 'simplecov'
SimpleCov.start do

0 comments on commit 24f759b

Please sign in to comment.