Tomas
Tomas

Reputation: 259

Rails 5 post file with Rest Client

I am trying to post a file in multipart data, from my server to an external server.

I am trying to perfect this action, when arriving on a page, so with the page controller for the action of this page:

Def page

    RestClient::Request.execute('https://.......', :file => File.new("app/assets/file/28000JAM20.344", 'rb'), headers={:type => 'file',
            :size => 40, :name => 'contact'})

end

So far i cannot manage to get anything posted, what I am missing?

Upvotes: 0

Views: 677

Answers (2)

Tomas
Tomas

Reputation: 259

Here is the solution:

  def page
    require 'rest-client'
    @filep = 'yourfilepath'

    request = RestClient::Request.new(
            :method => :post,
            :verify_ssl =>  OpenSSL::SSL::VERIFY_NONE,
            :url => 'your url',
            :payload => {
            :multipart => true,
            :file => File.new(@filep, 'rb')
          },
            :headers => {:type => 'file', :size => 40, :name => 'myfile'}
          )
    response = request.execute

  end

Upvotes: 0

Aniket Tiwari
Aniket Tiwari

Reputation: 3998

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

Upvotes: 1

Related Questions