Skip to content

Commit

Permalink
Allow application/x-www-form-url_encoded POST requests to use file ob…
Browse files Browse the repository at this point in the history
…jects as the request body (#1415)
  • Loading branch information
catlee committed May 6, 2022
1 parent 56d5844 commit a2b5f7f
Show file tree
Hide file tree
Showing 2 changed files with 15 additions and 2 deletions.
4 changes: 3 additions & 1 deletion lib/faraday/request/url_encoded.rb
Expand Up @@ -31,7 +31,9 @@ def match_content_type(env)
return unless process_request?(env)

env.request_headers[CONTENT_TYPE] ||= self.class.mime_type
yield(env.body) unless env.body.respond_to?(:to_str)
return if env.body.respond_to?(:to_str) || env.body.respond_to?(:read)

yield(env.body)
end

# @param env [Faraday::Env]
Expand Down
13 changes: 12 additions & 1 deletion spec/faraday/request/url_encoded_spec.rb
@@ -1,13 +1,19 @@
# frozen_string_literal: true

require 'stringio'

RSpec.describe Faraday::Request::UrlEncoded do
let(:conn) do
Faraday.new do |b|
b.request :url_encoded
b.adapter :test do |stub|
stub.post('/echo') do |env|
posted_as = env[:request_headers]['Content-Type']
[200, { 'Content-Type' => posted_as }, env[:body]]
body = env[:body]
if body.respond_to?(:read)
body = body.read
end
[200, { 'Content-Type' => posted_as }, body]
end
end
end
Expand Down Expand Up @@ -67,6 +73,11 @@
expect(response.body).to eq('a%5Bb%5D%5Bc%5D%5B%5D=d')
end

it 'works with files' do
response = conn.post('/echo', StringIO.new('str=apple'))
expect(response.body).to eq('str=apple')
end

context 'customising default_space_encoding' do
around do |example|
Faraday::Utils.default_space_encoding = '%20'
Expand Down

0 comments on commit a2b5f7f

Please sign in to comment.