From bbc4658b8f517969891026365a6434bdfdfe53ae Mon Sep 17 00:00:00 2001 From: Eric Chiang Date: Wed, 31 Dec 2025 09:21:24 -0500 Subject: [PATCH] Add trailing clrf when building header for non-file parts. When building a `StreamingMultipartBody`, the following example shows the header for the `first_name` part should be followed by a clrf before the part's content starts. ==== Before ==== --test-boundary-123 Content-Disposition: form-data; name="avatar"; filename="tiny.gif" Content-Type: image/gif GIF89a,; --test-boundary-123 Content-Disposition: form-data; name="first_name" John --test-boundary-123 ==== After ==== --test-boundary-123 Content-Disposition: form-data; name="avatar"; filename="tiny.gif" Content-Type: image/gif GIF89a,; --test-boundary-123 Content-Disposition: form-data; name="first_name" John --test-boundary-123 --- lib/httparty/request/streaming_multipart_body.rb | 2 ++ spec/httparty/request_spec.rb | 4 ++-- 2 files changed, 4 insertions(+), 2 deletions(-) diff --git a/lib/httparty/request/streaming_multipart_body.rb b/lib/httparty/request/streaming_multipart_body.rb index 2d02094c..fa087c29 100644 --- a/lib/httparty/request/streaming_multipart_body.rb +++ b/lib/httparty/request/streaming_multipart_body.rb @@ -135,6 +135,8 @@ def build_part_header(key, value, is_file) header << %(; filename="#{file_name(value).gsub(/["\r\n]/, replacement_table)}").b header << NEWLINE.b header << "Content-Type: #{content_type(value)}#{NEWLINE}".b + else + header << NEWLINE.b end header << NEWLINE.b header diff --git a/spec/httparty/request_spec.rb b/spec/httparty/request_spec.rb index ff9a05b5..c147e7fb 100644 --- a/spec/httparty/request_spec.rb +++ b/spec/httparty/request_spec.rb @@ -282,14 +282,14 @@ allow(HTTParty::Request::MultipartBoundary).to receive(:generate).and_return('test-boundary-123') # Get streaming content - request1 = HTTParty::Request.new(Net::HTTP::Post, 'http://api.foo.com/v1', body: { avatar: file }) + request1 = HTTParty::Request.new(Net::HTTP::Post, 'http://api.foo.com/v1', body: { avatar: file, first_name: "John" }) request1.send(:setup_raw_request) streaming_content = request1.instance_variable_get(:@raw_request).body_stream.read file.rewind # Get non-streaming content - request2 = HTTParty::Request.new(Net::HTTP::Post, 'http://api.foo.com/v1', body: { avatar: file }, stream_body: false) + request2 = HTTParty::Request.new(Net::HTTP::Post, 'http://api.foo.com/v1', body: { avatar: file, first_name: "John" }, stream_body: false) request2.send(:setup_raw_request) non_streaming_content = request2.instance_variable_get(:@raw_request).body