1
0
Fork 0
mirror of https://github.com/jnunemaker/httparty synced 2023-03-27 23:23:07 -04:00

Determine content_type for multipart payload

This commit is contained in:
 TheSmartnik 2018-10-20 19:49:14 +03:00
parent c92336bb7c
commit 5820992bc5
4 changed files with 12 additions and 4 deletions

View file

@ -16,6 +16,7 @@ Gem::Specification.new do |s|
s.required_ruby_version = '>= 2.0.0' s.required_ruby_version = '>= 2.0.0'
s.add_dependency 'multi_xml', ">= 0.5.2" s.add_dependency 'multi_xml', ">= 0.5.2"
s.add_dependency('mime-types', "~> 3.0")
# If this line is removed, all hard partying will cease. # If this line is removed, all hard partying will cease.
s.post_install_message = "When you HTTParty, you must party hard!" s.post_install_message = "When you HTTParty, you must party hard!"

View file

@ -4,6 +4,7 @@ require 'net/https'
require 'uri' require 'uri'
require 'zlib' require 'zlib'
require 'multi_xml' require 'multi_xml'
require 'mime/types'
require 'json' require 'json'
require 'csv' require 'csv'

View file

@ -35,9 +35,9 @@ module HTTParty
memo += %(Content-Disposition: form-data; name="#{key}") memo += %(Content-Disposition: form-data; name="#{key}")
# value.path is used to support ActionDispatch::Http::UploadedFile # value.path is used to support ActionDispatch::Http::UploadedFile
# https://github.com/jnunemaker/httparty/pull/585 # https://github.com/jnunemaker/httparty/pull/585
memo += %(; filename="#{determine_file_name(value)}") if file?(value) memo += %(; filename="#{file_name(value)}") if file?(value)
memo += "\r\n" memo += "\r\n"
memo += "Content-Type: application/octet-stream\r\n" if file?(value) memo += "Content-Type: #{content_type(value)}\r\n" if file?(value)
memo += "\r\n" memo += "\r\n"
memo += file?(value) ? value.read : value.to_s memo += file?(value) ? value.read : value.to_s
memo += "\r\n" memo += "\r\n"
@ -74,7 +74,13 @@ module HTTParty
end end
end end
def determine_file_name(object) def content_type(object)
return object.content_type if object.respond_to?(:content_type)
mime = MIME::Types.type_for(file_name(object))
mime.empty? ? 'application/octet-stream' : mime[0].content_type
end
def file_name(object)
object.respond_to?(:original_filename) ? object.original_filename : File.basename(object.path) object.respond_to?(:original_filename) ? object.original_filename : File.basename(object.path)
end end

View file

@ -38,7 +38,7 @@ RSpec.describe HTTParty::Request::Body do
end end
let(:expected_file_name) { 'tiny.gif' } let(:expected_file_name) { 'tiny.gif' }
let(:expected_file_contents) { "GIF89a\u0001\u0000\u0001\u0000\u0000\xFF\u0000,\u0000\u0000\u0000\u0000\u0001\u0000\u0001\u0000\u0000\u0002\u0000;" } let(:expected_file_contents) { "GIF89a\u0001\u0000\u0001\u0000\u0000\xFF\u0000,\u0000\u0000\u0000\u0000\u0001\u0000\u0001\u0000\u0000\u0002\u0000;" }
let(:expected_content_type) { 'application/octet-stream' } let(:expected_content_type) { 'image/gif' }
let(:multipart_params) do let(:multipart_params) do
"--------------------------c772861a5109d5ef\r\n" \ "--------------------------c772861a5109d5ef\r\n" \
"Content-Disposition: form-data; name=\"user[avatar]\"; filename=\"#{expected_file_name}\"\r\n" \ "Content-Disposition: form-data; name=\"user[avatar]\"; filename=\"#{expected_file_name}\"\r\n" \