ruby 使用法拉第上传文件

wooyq4lh  于 2023-10-17  发布在  Ruby
关注(0)|答案(4)|浏览(115)

我尝试使用法拉第将文件上传到Web服务时遇到问题。
我的代码:

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

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

conn.post('/', payload)

似乎没有什么事情发生后,尝试的职位。当我检查响应this时,我看到的是:

#<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对象,它似乎永远不会发出请求。

j91ykkif

j91ykkif1#

原来我需要指定一个适配器。这是最终工作的代码。

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)
sd2nnvve

sd2nnvve2#

这样就解决了我的问题:

conn = Faraday.new(url: URL) do |faraday|
  faraday.request :multipart #make sure this is set before url_encoded
  faraday.request :url_encoded
  faraday.adapter Faraday.default_adapter
end

file = Faraday::UploadIO.new(
  params[:image].tempfile.path,
  params[:image].content_type,
  params[:image].original_filename
)

payload = { :file => file }

conn.post('/', payload)
mtb9vblg

mtb9vblg3#

v0.16.0之后的法拉第:https://lostisland.github.io/faraday/middleware/multipart
“Multipart”中间件检测文件并使用“multipart/form-data”进行编码:

payload[:profile_pic] = Faraday::FilePart.new('/path/to/avatar.jpg', 'image/jpeg')

conn.put '/profile', payload
6uxekuva

6uxekuva4#

如果您使用带有默认net_http适配器的法拉第,并且不想使用multipart中间件上传文件,则可以将其改为流式传输:

conn = Faraday.new(url: "https://example.org")

conn.put(
  "/upload",
  File.open(src_path, "rb"),
  {
    "Content-Length" => File.size(src_path).to_s,
    "Transfer-Encoding" => "chunked",
    "Content-Type" => "application/octet-stream"
  }
)

这在引擎盖下使用了Net::HTTPGenericRequest#body_stream
(使用法拉第v2.2.0测试)

相关问题