Rails, RestClient pass binary font file - ruby-on-rails

I'm trying to use restclient to do this CURL post
curl -v -i -X POST -H 'X-Api-Secret: ' -F font=#myraid.ttf APIURL
This is what I have so far
response = RestClient::Request.execute(
:method => :post,
:url => api_url,
:headers => {:content_type => 'multipart/form-data', :accept => 'application/json', :X_Api_Secret => "xxxxxxxxx"}
)
which works perfectly until I try to pass a file like this
response =RestClient::Request.execute(
:method => :post,
:url => api_url,
:headers => {:content_type => 'multipart/form-data', :accept => 'application/json', :X_Api_Secret => "xxxxxxxxxx"},
:upload => {
:file => File.new(params[:custom_font][:font].path, 'rb')
}
)
It then seems to ignore my headers and I get a 403 error, I've also tried a regular rest-client post request like
response = RestClient.post api_url, { :X_Api_Secret => "xxxxxx"}
But again it breaks when I try to introduce the data file.
Any help, pointers or advice would be greatly appreciated.

Ok after a couple of days of head banging, I ended up using unirest instead, after updating to the latest version it worked straight away.
response = Unirest.post api_key,
headers:{
"X-Api-Secret" => "xxxxx"
},
parameters:{
"data" => "ccc"
}

Related

Sending the parent object to the Box File Upload API in Ruby on Rails

I'm attempting to use the Box File Upload API to send a file to Box.com from a Ruby on Rails application. However, I keep getting a bad request response that says parent is a missing parameter.
Here is their documentation:
https://developer.box.com/v2.0/reference#upload-a-file
Here is their curl example:
curl https://upload.box.com/api/2.0/files/content \
-H "Authorization: Bearer ACCESS_TOKEN" -X POST \
-F attributes='{"name":"tigers.jpeg", "parent":{"id":"11446498"}}' \
-F file=#myfile.jpg
I'm attempting to use RailsClient to do this in my app and here are the two ways I've tried:
RestClient.post(BASE_CONTENT_URL,{ :name => "randompdf.pdf", :parent => {:id => 0}, :myfile => file }, { :Authorization => "Bearer #{NEW_TOKEN}" })
&
#parent = Struct.new(:id)
#parent.id = 0
RestClient.post(BASE_CONTENT_URL,{ :name => "randompdf.pdf", :parent => #parent, :myfile => file }, { :Authorization => "Bearer #{NEW_TOKEN}" })
I get the same error attempting to do it via Postman as well. Box's Community Forum and Support resources haven't gotten back to me so I'm a little lost.
attributes = {name: #{unique_name}, parent: { id: 0 }}
box_response = RestClient::Request.execute(method: :post,
url: BASE_UPLOAD_URL + "/content",
payload: { attributes: attributes.to_json,
file: file},
headers: {Authorization: "Bearer #{NEW_TOKEN}"})

Trying to call third party api through rest-client giving RestClient::SSLCertificateNotVerified error

I am trying to call rest api of https://example.com but getting above error pls help me with the same
To bypass verify SSL certificate you can use :verify_ssl => false like this
RestClient::Request.execute(:url => 'https://example.com/', :method => :get, :verify_ssl => false)
Just in case that doesn't work also put :proxy => nil like this
RestClient::Request.execute(:url => 'https://example.com/', :method => :get, :verify_ssl => false, :proxy => nil)

Add parameters to post request with Rest-client rails

I need to make a Post request using Rest-Client in my rails backend. I can successfully do it by the following: rv = RestClient.post URL, id.to_json, :content_type => 'application/json'
I need to add a tag parameter but can't make it work. I've tried different combinations of the following: rv = RestClient.post URL, {:params => {:tag => 'TAG'}}, id.to_json, :content_type => 'application/json' and receive errors about syntax (wrong number of arguments). The params would go on the url, the id.to_json would be part of the body. I can't find documentation that talks about this specific use case.
You can use to_query to convert a hash into a HTTP query string like so:
url_params = {:tag => "TAG"}.to_query
=> "tag=TAG"
Then just use that to construct your entire URL:
rv = RestClient.post "#{URL}?#{url_params}", id.to_json, :content_type => 'application/json'
there is another way to add url parameters to the request, it is notable that you have not sent the post parameters to RestClient in the correct order, which is url , payload, headers. and the headers can include any other tag, like :params.
rv = RestClient.post(URL, id.to_json, {:params => {:tag => 'TAG'},
:content_type => 'application/json' })

HTTParty not sending data to API

I am trying to call a Restful API (basically a third party application). Here is my code:
resp = HTTParty.post("http://example.com/test/api/url",
{
:query => {"id"=> 3, "example_payload"=> "test payload", "details"=> {}},
:headers => { 'Content-Type' => 'application/json', 'Accept' => 'application/json', 'client_key' => "xxx-xxx-xxxxx" }
})
This is returning a response with status "200 ok". But I am not able to find the changes in my third party application.
I tried with "Rest-client" and "Net-http" as well. Both of them are also not working.
Thanks for helping in advance.
I assume that by
"not able to find the changes in my third party application"
, you mean that your not able to hit that third party url.
Solution:
You don't need to wrap the query and headers into a hash. Simply pass them as parameters like:
HTTParty.post(
"http://example.com/test/api/url",
query: {"id"=> 3, "example_payload"=> "test payload", "details"=> {}},
headers: { 'Content-Type' => 'application/json',
'Accept' => 'application/json',
'client_key' => "xxx-xxx-xxxxx"
}
)
This works for me.

Changing Content-Type to JSON using HTTParty

I am trying to use Ruby on Rails to communicate with the Salesforce API. I can fetch data easily enough but I am having problems posting data to the server. I am using HTTParty as per Quinton Wall's post here:
https://github.com/quintonwall/omniauth-rails3-forcedotcom/wiki/Build-Mobile-Apps-in-the-Cloud-with-Omniauth,-Httparty-and-Force.com
but all I seem to be able to get from the salesforce server is the error that I am submitting the body as html
{"message"=>"MediaType of 'application/x-www-form-urlencoded' is not supported by this resource", "errorCode"=>"UNSUPPORTED_MEDIA_TYPE"}
the responsible code looks like:
require 'rubygems'
require 'httparty'
class Accounts
include HTTParty
format :json
...[set headers and root_url etc]
def self.save
Accounts.set_headers
response = (post(Accounts.root_url+"/sobjects/Account/", :body => {:name => "graham"}.to_json))
end
end
anyone have an idea why the body should be being posted as html and how to change this so that it definitely goes as json so that salesforce doesn't reject it?
Any help would be appreciated. cheers
The Content-Type header needs to be set to "application/json". This can be done by inserting :headers => {'Content-Type' => 'application/json'} as a parameter to post, ie:
response = post(Accounts.root_url+"/sobjects/Account/",
:body => {:name => "graham"}.to_json,
:headers => {'Content-Type' => 'application/json'} )
You have to set the Content-Type header to application/json. I haven't used HTTParty, but it looks like you have to do something like
response = (post(Accounts.root_url+"/sobjects/Account/", :body => {:name => "graham"}.to_json) , :options => { :headers => { 'Content-Type' => 'application/json' } } )
I'm somewhat surpised that the format option doesn't do this automatically.

Resources