我试图使用faraday(带有默认适配器)将一些二进制数据放到url中。
我试图使用curl执行的请求是:
curl -X PUT https://your_user_token:@api.80legs.com/v2/urllists/name_of_url_list -H "Content-Type: application/octet-stream" --data-binary "[\"http://www.example.com/\", \"http://www.sample.com/\", \"http://www.test.com/\"]" -i
在法拉第,我提出的要求可能是错误的:
require 'tempfile'
tempfile = Tempfile.new(name)
tempfile.write url_list.to_s
conn = Faraday.new(url: "https://#{token}:@api.80legs.com/v2/urllists/#{name}") do |faraday|
faraday.request :multipart
faraday.response :logger
end
conn.put do |req|
req.headers['Content-Type'] = 'octet/stream'
req.body = Faraday::UploadIO.new(tempfile, 'octet/stream')
end
tempfile.close
以前有人用这个宝石做过这样的事情吗?
下一代
最佳答案
取自https://github.com/lostisland/faraday/issues/380
require 'tempfile'
tempfile = Tempfile.new(name)
tempfile.write url_list.to_s
conn = Faraday.new(url: "https://#{token}:@api.80legs.com/v2/urllists/#{name}") do |faraday|
faraday.request :multipart
faraday.response :logger
faraday.adapter :net_http
end
conn.put do |req|
req.headers['Content-Type'] = 'octet/stream'
req.body = Faraday::UploadIO.new(tempfile, 'octet/stream')
end
tempfile.close