Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
5 changes: 1 addition & 4 deletions lib/httparty/request.rb
Original file line number Diff line number Diff line change
Expand Up @@ -242,11 +242,8 @@ def setup_raw_request
force_multipart: options[:multipart]
)

if body.multipart?
content_type = "multipart/form-data; boundary=#{body.boundary}"
@raw_request['Content-Type'] = content_type
end
@raw_request.body = body.call
@raw_request['Content-Type'] ||= body.content_type
end

@raw_request.instance_variable_set(:@decode_content, decompress_content?)
Expand Down
26 changes: 21 additions & 5 deletions lib/httparty/request/body.rb
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,12 @@ def initialize(params, query_string_normalizer: nil, force_multipart: false)

def call
if params.respond_to?(:to_hash)
multipart? ? generate_multipart : normalize_query(params)
if multipart?
@content_type = "multipart/form-data; boundary=#{boundary}"
generate_multipart
else
normalize_query_and_set_content_type(params)
end
Comment on lines +19 to +24
Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

useless change

Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

cus generate_multipiart sets @content_type, too

else
params
end
Expand All @@ -30,6 +35,13 @@ def multipart?
params.respond_to?(:to_hash) && (force_multipart || has_file?(params))
end

def content_type
Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

it acts as a getter when set, and tries to calc when body is not yet evaluated.

return @content_type if defined?(@content_type)
return "multipart/form-data; boundary=#{boundary}" if multipart?

"application/x-www-form-urlencoded"
end

private

# https://html.spec.whatwg.org/#multipart-form-data
Expand All @@ -54,7 +66,8 @@ def generate_multipart
memo << content_body(value)
memo << NEWLINE.b
end


@content_type = "multipart/form-data; boundary=#{boundary}"
multipart << "--#{boundary}--#{NEWLINE}".b
end

Expand All @@ -72,10 +85,12 @@ def file?(object)
object.respond_to?(:path) && object.respond_to?(:read)
end

def normalize_query(query)
def normalize_query_and_set_content_type(query)
if query_string_normalizer
query_string_normalizer.call(query)
query, @content_type = query_string_normalizer.call(query)
query
else
@content_type = 'application/x-www-form-urlencoded'
HashConversions.to_params(query)
end
end
Expand All @@ -91,8 +106,9 @@ def content_body(object)
end
end

def content_type(object)
def object_content_type(object)
return object.content_type if object.respond_to?(:content_type)

require 'mini_mime'
mime = MiniMime.lookup_by_filename(object.path)
mime ? mime.content_type : 'application/octet-stream'
Expand Down