Ruby 如何用 httpclient 来上传一个文件

rubyfans001 · January 11, 2015 · Last by guojhq replied at March 29, 2024 · 1800 hits

有一个二进制文件,需要用 PUT 方法来上传到服务器端,请问利用 httpclient 该如何实现呢?

谢谢 lidashuang,我来学习下这个帖子,大家继续支招啊!

可以用 Faraday

https://github.com/lostisland/faraday

conn = Faraday.new(url: "http://youapp.com") do |conn|
  # POST/PUT params encoders:
  conn.request :multipart
  conn.request :url_encoded
  conn.adapter :net_http
end

payload[:file] = Faraday::UploadIO.new('/path/to/avatar.jpg', 'image/jpeg')
conn.put '/upload', payload

在 Faraday 的 README 页面以及有非常多的例子了

现在没了😓

You need to Sign in before reply, if you don't have an account, please Sign up first.