Put binary-data with faraday

Discussion in 'Ruby' started by ngw, May 28, 2014.

  1. ngw

    ngw Guest

    Hi everyone, 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}:mad:api.80legs.com/v2/urllists/#{name}") do |faraday|
    faraday.request :multipart
    faraday.response :logger
    conn.put do |req|
    req.headers['Content-Type'] = 'octet/stream'
    req.body = Faraday::UploadIO.new(tempfile, 'octet/stream')

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

    ngw, May 28, 2014
    1. Advertisements

  2. Did you try to set the encoding here?

    tempfile.set_encoding 'BINARY'


    Robert Klemme, May 28, 2014
    1. Advertisements

  3. PS: Maybe you also need to seek after writing to ensure reading starts at the beginning.
    Robert Klemme, May 28, 2014
    1. Advertisements

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments (here). After that, you can post your question and our members will help you out.