Skip to content
Merged
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
2 changes: 2 additions & 0 deletions lib/httparty/request.rb
Original file line number Diff line number Diff line change
Expand Up @@ -245,6 +245,8 @@ def setup_raw_request
if body.multipart?
content_type = "multipart/form-data; boundary=#{body.boundary}"
@raw_request['Content-Type'] = content_type
elsif options[:body].respond_to?(:to_hash) && !@raw_request['Content-Type']
@raw_request['Content-Type'] = 'application/x-www-form-urlencoded'
end
@raw_request.body = body.call
end
Expand Down
23 changes: 23 additions & 0 deletions spec/httparty/request_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -378,6 +378,29 @@
end
end
end

context "when body is a Hash" do
subject(:headers) do
@request.send(:setup_raw_request)
headers = @request.instance_variable_get(:@raw_request).each_header.to_a
Hash[*headers.flatten]
end

it "sets header Content-Type: application/x-www-form-urlencoded" do
@request.options[:body] = { foo: 'bar' }

expect(headers['content-type']).to eq('application/x-www-form-urlencoded')
end

context "and header Content-Type is provided" do
it "does not overwrite the provided Content-Type" do
@request.options[:body] = { foo: 'bar' }
@request.options[:headers] = { 'Content-Type' => 'application/json' }

expect(headers['content-type']).to eq('application/json')
end
end
end
end

describe 'http' do
Expand Down