Skip to content

Commit

Permalink
Multipart/Form-Data: rewind files after read
Browse files Browse the repository at this point in the history
Fixes #708
  • Loading branch information
gi committed Sep 25, 2020
1 parent 0e8b827 commit 21ae5cf
Show file tree
Hide file tree
Showing 2 changed files with 12 additions and 1 deletion.
11 changes: 10 additions & 1 deletion lib/httparty/request/body.rb
Original file line number Diff line number Diff line change
Expand Up @@ -39,7 +39,7 @@ def generate_multipart
memo += "\r\n"
memo += "Content-Type: #{content_type(value)}\r\n" if file?(value)
memo += "\r\n"
memo += file?(value) ? value.read : value.to_s
memo += content_body(value)
memo += "\r\n"
end

Expand Down Expand Up @@ -68,6 +68,15 @@ def normalize_query(query)
end
end

def content_body(object)
if file?(object)
object = (file = object).read
file.rewind if file.respond_to?(:rewind)
end

object.to_s
end

def content_type(object)
return object.content_type if object.respond_to?(:content_type)
mime = MIME::Types.type_for(object.path)
Expand Down
2 changes: 2 additions & 0 deletions spec/httparty/request/body_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -62,6 +62,8 @@

it { is_expected.to eq multipart_params }

it { expect { subject }.not_to change { file.pos } }

context 'when passing multipart as an option' do
let(:options) { { force_multipart: true } }
let(:params) do
Expand Down

0 comments on commit 21ae5cf

Please sign in to comment.