7

Faraday を使用して Web サービスにファイルをアップロードしようとすると問題が発生します。

私のコード:

conn = Faraday.new('http://myapi') do |f|
  f.request :multipart
end

payload = { :file => Faraday::UploadIO.new('...', 'image/jpeg') }

conn.post('/', payload)

投稿を試みた後、何も起こらないようです。応答を調べると、のようになります。

#<Faraday::Response:0x007fd9e5903870 @env={:method=>:post, :body=>#<Faraday::CompositeReadIO:0x007fd9e5903a50 @parts=[#<Parts::FilePart:0x007fd9e5903e38 @head="-------------RubyMultipartPost\r\nContent-Disposition: form-data; name=\"file\"; filename=\"indymade-badge-horizontal.png\"\r\nContent-Length: 7821\r\nContent-Type: image/png\r\nContent-Transfer-Encoding: binary\r\n\r\n", @foot="\r\n", @length=8026, @io=#<CompositeReadIO:0x007fd9e5903b68 @ios=[#<StringIO:0x007fd9e5903bb8>, #<UploadIO:0x007fd9e514a3b8 @content_type="image/png", @original_filename="indymade-badge-horizontal.png", @local_path="/Users/anthonator/Downloads/indymade-badge-horizontal.png", @io=#<File:/Users/anthonator/Downloads/indymade-badge-horizontal.png>, @opts={}>, #<StringIO:0x007fd9e5903b90>], @index=0>>, #<Parts::EpiloguePart:0x007fd9e5903ac8 @part="-------------RubyMultipartPost--\r\n\r\n", @io=#<StringIO:0x007fd9e5903a78>>], @ios=[#<CompositeReadIO:0x007fd9e5903b68 @ios=[#<StringIO:0x007fd9e5903bb8>, #<UploadIO:0x007fd9e514a3b8 @content_type="image/png", @original_filename="indymade-badge-horizontal.png", @local_path="/Users/anthonator/Downloads/indymade-badge-horizontal.png", @io=#<File:/Users/anthonator/Downloads/indymade-badge-horizontal.png>, @opts={}>, #<StringIO:0x007fd9e5903b90>], @index=0>, #<StringIO:0x007fd9e5903a78>], @index=0>, :url=>#<URI::HTTPS:0x007fd9e5909d60 URL:https://myapi>, :request_headers=>{"User-Agent"=>"Faraday v0.8.7", "Content-Type"=>"multipart/form-data;boundary=-----------RubyMultipartPost", "Content-Length"=>"8062"}, :parallel_manager=>nil, :request=>{:proxy=>nil, :boundary=>"-----------RubyMultipartPost"}, :ssl=>{}, :response=>#<Faraday::Response:0x007fd9e5903870 ...>}, @on_complete_callbacks=[]>
irb(main):065:0> response.inspect
=> "#<Faraday::Response:0x007fd9e5903870 @env={:method=>:post, :body=>#<Faraday::CompositeReadIO:0x007fd9e5903a50 @parts=[#<Parts::FilePart:0x007fd9e5903e38 @head=\"-------------RubyMultipartPost\\r\\nContent-Disposition: form-data; name=\\\"file\\\"; filename=\\\"indymade-badge-horizontal.png\\\"\\r\\nContent-Length: 7821\\r\\nContent-Type: image/png\\r\\nContent-Transfer-Encoding: binary\\r\\n\\r\\n\", @foot=\"\\r\\n\", @length=8026, @io=#<CompositeReadIO:0x007fd9e5903b68 @ios=[#<StringIO:0x007fd9e5903bb8>, #<UploadIO:0x007fd9e514a3b8 @content_type=\"image/png\", @original_filename=\"indymade-badge-horizontal.png\", @local_path=\"/Users/anthonator/Downloads/indymade-badge-horizontal.png\", @io=#<File:/Users/anthonator/Downloads/indymade-badge-horizontal.png>, @opts={}>, #<StringIO:0x007fd9e5903b90>], @index=0>>, #<Parts::EpiloguePart:0x007fd9e5903ac8 @part=\"-------------RubyMultipartPost--\\r\\n\\r\\n\", @io=#<StringIO:0x007fd9e5903a78>>], @ios=[#<CompositeReadIO:0x007fd9e5903b68 @ios=[#<StringIO:0x007fd9e5903bb8>, #<UploadIO:0x007fd9e514a3b8 @content_type=\"image/png\", @original_filename=\"indymade-badge-horizontal.png\", @local_path=\"/Users/anthonator/Downloads/indymade-badge-horizontal.png\", @io=#<File:/Users/anthonator/Downloads/indymade-badge-horizontal.png>, @opts={}>, #<StringIO:0x007fd9e5903b90>], @index=0>, #<StringIO:0x007fd9e5903a78>], @index=0>, :url=>#<URI::HTTPS:0x007fd9e5909d60 URL:https://myapi>, :request_headers=>{\"User-Agent\"=>\"Faraday v0.8.7\", \"Content-Type\"=>\"multipart/form-data;boundary=-----------RubyMultipartPost\", \"Content-Length\"=>\"8062\"}, :parallel_manager=>nil, :request=>{:proxy=>nil, :boundary=>\"-----------RubyMultipartPost\"}, :ssl=>{}, :response=>#<Faraday::Response:0x007fd9e5903870 ...>}, @on_complete_callbacks=[]>"

本体は CompositeReadIO オブジェクトに設定されており、リクエストを送信していないようです。

4

3 に答える 3

20

アダプターを指定する必要があることがわかりました。最終的に動作したコードは次のとおりです。

conn = Faraday.new('http://myapi') do |f|
  f.request :multipart
  f.request :url_encoded
  f.adapter :net_http # This is what ended up making it work
end

payload = { :file => Faraday::UploadIO.new('...', 'image/jpeg') }

conn.post('/', payload)
于 2013-06-01T00:03:01.160 に答える