用法拉第放二进制数据

时间:2014-05-28 16:07:48

标签: ruby http faraday

我正在尝试使用法拉第(使用默认适配器)将一些二进制数据放入网址。

我正在尝试使用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

有人设法在使用这个宝石之前做过类似的事吗?

NGW

1 个答案:

答案 0 :(得分:1)

取自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