1

I'm trying to use faraday (with default adapter) to put some binary-data to an url.

The request I'm trying to do using curl is:

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

In Faraday I'm setting up the request probably in the wrong way:

  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

Has someone managed to do something like this before with this gem?

ngw

ngw
  • 1,222
  • 1
  • 14
  • 34

1 Answers1

1

Taken from 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
Peter Souter
  • 5,110
  • 1
  • 33
  • 62