Ruby Rest-Client File Upload as Multipart Form Data with Basic Authenticaion

Ruby rest-client file upload as multipart form data with basic authenticaion

How about using a RestClient::Payload with RestClient::Request...
For an example:

request = RestClient::Request.new(
:method => :post,
:url => '/data',
:user => @sid,
:password => @token,
:payload => {
:multipart => true,
:file => File.new("/path/to/image.jpg", 'rb')
})
response = request.execute

Ruby Multi-part Form Upload with RestClient

Try to use :headers => {:authorization => "BoxAuth api_key=<API_KEY>&auth_token=<AUTH_TOKEN>"} in the call. That should fix the missing authorization header.

Complete request would then be:

request = RestClient::Request.new(:method => :post,:url => "https://www.box.com/api/2.0/files/data",:headers => {:authorization => "BoxAuth api_key=<API_KEY>&auth_token=<AUTH_TOKEN>"},:filename => "test.txt", :payload => { :multipart => true, :file => File.new("test.txt")})

Using restclient with multipart posts

I doubt you can really pass a CGI-style upload param from Rails into restclient and expect it to work.

A regular upload in Rails would have quite some extra attributes which do not belong in a posted resource (like the original filename and so on), and a Rails upload contains an IO with the actual file data. Also a file upload object in Rails might be a Tempfile handle and might be a StringIO - depending on the size of the upload.

What you effectively need to do is "repackage" your upload for rest-client to handle it properly, and pass the repackaged and rewound Tempfile object to restclient. Maybe you can get away with just picking the upload object itself instead of the whole params[:file]

Ruby: How to post a file via HTTP as multipart/form-data?

I like RestClient. It encapsulates net/http with cool features like multipart form data:

require 'rest_client'
RestClient.post('http://localhost:3000/foo',
:name_of_file_param => File.new('/path/to/file'))

It also supports streaming.

gem install rest-client will get you started.

(Multipart) zip upload in ruby webmachine handled by rack

I've created a gist which shows how to retrieve the multipart stream. You'll need further parsing in order to get the uploaded file.

https://gist.github.com/jewilmeer/eb40abd665b70f53e6eb60801de24342

Rails 5 post file with Rest Client

Firstly the Def keyword is always in small letters. Now lets talk about problem you have not specified the request type. According to me it must be post request type. So try the below solution

    def page
request = RestClient::Request.new(
:method => :post,
:url => 'https://.......'
:payload => {
:multipart => true,
:file => File.new("app/assets/file/28000JAM20.344", 'rb')
})
response = request.execute
end


Related Topics



Leave a reply



Submit