Mahesh Pawar
Mahesh Pawar

Reputation: 153

How to convert following curl code to ruby code with restclient

curl http://192.168.253.7:8080/Example-1/restful/data/bulkUpload -H "API_KEY: se5ed3635ee24bb89dd72f" -H "TOKEN:1221231233" -F file=~/Desktop/Sample Import.xlsx

And it works fine through terminal.

I have above curl code and converted it to ruby code as follows:

require 'rest-client'
RestClient.post(url,
                  :file => File.new(file_path),
                  headers: {'API_KEY' => Settings.API_KEY, 'TOKEN' => Settings.TOKEN})

But still i am getting API _KEY is not in header response?

Am I missing something here?

Thanks.

Upvotes: 0

Views: 530

Answers (1)

Mahesh Pawar
Mahesh Pawar

Reputation: 153

As we have to specify the headers before the payload. And the API_KEY and TOKEN should be inside the headers section.
So following RestClient request is the solution for my question.

  request = RestClient::Request.new(
      :method => :post,
      :url => url,
      :headers => {'API_KEY' => Settings.API_KEY,'TOKEN' => '123123123'},
      :payload => {
          :multipart => true,
          :file => File.new(file_path, 'rb')
      })
  response = request.execute

Upvotes: 1

Related Questions