I do this curl in Terminal which works excellent:
$ curl https://myurl.com/api/v1/orders/53e0ae7f6630361c46060000 -H "Authorization: Token xxxxxxxxxxxxxxxxxxxxxx"
Output is json.
Now I want to access the json string via my rails app. I have tried RestClient to do this, but somehow I always get a 401 unauthorized error. I believe the token gets not send correctly via header. I have tried the following:
RestClient.get 'https://myurl.com/api/v1/orders/53e0ae7f6630361c46060000', {token: 'xxxxxxxxxxxxxxxxxxxxxx'}
and
RestClient.get 'https://myurl.com/api/v1/orders/53e0ae7f6630361c46060000', :params => {:token => 'xxxxxxxxxxxxxxxxxxxxxx'}
with no success. Maybe I use a wrong syntax for sending the token in the header?
Doku is here http://rubydoc.info/github/rest-client/rest-client - I could not find any mistakes.
# GET request with modified headers
RestClient.get 'http://example.com/resource', {:Authorization => 'Bearer cT0febFoD5lxAlNAXHo6g'}
# POST request with modified headers
RestClient.post 'http://example.com/resource', {:foo => 'bar', :baz => 'qux'}, {:Authorization => 'Bearer cT0febFoD5lxAlNAXHo6g'}
# DELETE request with modified headers
RestClient.delete 'http://example.com/resource', {:Authorization => 'Bearer cT0febFoD5lxAlNAXHo6g'}
Source: https://github.com/rest-client/rest-client#headers
Related
I am trying to get the API Token from this api.
https://docs.bigmarker.com/#login
Basically below is the API format and it works with PostMan as well
curl -i -X POST --data "email=youremail#email.com&password=yourpassword" https://www.bigmarker.com/api/v1/members/login
I didn't specify any Headers, Just use the POST form data with email & password.
And this is my rails code.
require 'net/http'
require 'net/https'
uri = URI("https://www.bigmarker.com/api/v1/members/login")
res = Net::HTTP.post_form(uri, {'email' => email, 'passowrd' => password})
But it returns HTTP 500 Error.
#<Net::HTTPInternalServerError 500 Internal Server Error readbody=true>
Any idea?
Thanks
My mistake.
There was a spelling error.
res = Net::HTTP.post_form(uri, {'email' => email, 'passowrd' => password})
'passowrd' should be 'password'
I'm try to post a file using RestClient with Ruby on rails, I have try to conver below curl command:
curl -H 'Authorization: Bearer ACCESS_TOKEN' -F 'file=#FILE_PATH' https://api.com/api
When converting with RestClient like below:
res = RestClient::Request.new(
method: :post,
url: 'https://api.com/api',
:payload => {
:file => File.new("#{Rails.root}/public/Sample.docx" , 'rb')
},
:headers => {
:Authorization => "Bearer 155c5e5c4feea51b386fdfe204abc60b7f4025fb91988519de869525217b4702",
:content_type => "multipart/form-data"
}
)
res.execute
On this code showing 400 Bad Request
What I'm doing wrong with this code?
Thanks
Your post is a duplicate of this one Ruby - Uploading a file using RestClient post
In your case, try changing content_type to application/vnd.openxmlformats-officedocument.wordprocessingml.document
List of MIME types can be found here: What is a correct mime type for docx, pptx etc?
I'm building an app in Rails using the Spotify web API. I built a method to refresh a user's token, but am receiving a 400 error. According the the Spotify Web API docs, the header of my request needs to be in the following format:
Authorization: Basic <base64 encoded client_id:client_secret>
Using Httparty gem, here's the POST method to refresh the access token:
def refresh_token
client_id = "foo"
client_secret = "bar"
client_id_and_secret = Base64.encode64("#{client_id}:#{client_secret}")
result = HTTParty.post(
"https://accounts.spotify.com/api/token",
:body => {:grant_type => "refresh_token",
:refresh_token => "#{self.oauth_refresh_token}"},
:headers => {"Authorization" => "Basic #{client_id_and_secret}"}
)
end
Here's what "result" ends up being:
=> #<HTTParty::Response:0x7f92190b2978 parsed_response={"error"=>"invalid_client", "error_description"=>"Invalid client secret"}, #response=#<Net::HTTPBadRequest 400 Bad Request readbody=true>, #headers={"server"=>["nginx"], "date"=>["Sun, 31 Aug 2014 22:28:38 GMT"], "content-type"=>["application/json"], "content-length"=>["70"], "connection"=>["close"]}>
I can decode client_id_and_secret and it returns "foo:bar", so I'm at a loss as to why I'm receiving a 400 error. Any insight is much appreciated.
Found the issue... it was with the Base64 encoding in Ruby. Apparently (as shown in Strange \n in base64 encoded string in Ruby) using the Base64.encode64('') method adds an extra line within the code. Using Base64.strict_encode64('') solved the issue.
Updated code:
def refresh_token
client_id = "foo"
client_secret = "bar"
client_id_and_secret = Base64.strict_encode64("#{client_id}:#{client_secret}")
result = HTTParty.post(
"https://accounts.spotify.com/api/token",
:body => {:grant_type => "refresh_token",
:refresh_token => "#{self.oauth_refresh_token}"},
:headers => {"Authorization" => "Basic #{client_id_and_secret}"}
)
end
Somehow HTTParty returns 401 where CURL works fine. Not sure how to pass token in headers.
Working (200):
curl http://localhost:3020/api/products -H 'Authorization: Token token="111"'
Not working (401):
HTTParty.get('http://localhost:3020/api/products', headers: {"Authorization: Token token" => '111'})
I have tried with just "Authorization" => '111'and "token" => '111' but same result.
Managed to get it working as follows.
HTTParty.get("http://localhost:3020/api/products", headers: {"Authorization" => "Token token=\"111\""})
This also works if you want to set headers of the class dynamically, this example is for obtaining the Authorization token for Dun and Bradstreet
require 'httparty'
require 'certified'
class DnbAuth
include HTTParty
debug_output $stdout
base_uri "https://maxcvservices.dnb.com/rest/Authentication"
def initialize(ct,u,p)
self.class.headers 'Content-type' => "#{ct}"
self.class.headers 'x-dnb-user' => "#{u}"
self.class.headers 'x-dnb-pwd'=> "#{p}"
end
def token()
response = self.class.post("/")
end
end
ct = 'text/xml'
u = 'xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx'
p = 'xxxxxx'
xx = DnbAuth.new(ct,u,p)
puts xx.token.message
I'm using rails4 + rspec 3. I want to make HTTP calls, and pass both params (such as JSON body or query string), and also HTTP headers. I was able to pass one of these two, but not both.
when I try something like:
post api_v1_post_path(#myid), {} , {"X-Some-Header" => "MyValue"}
it works fine and the headers fine, but if I do something like:
post api_v1_post_path(#myid), {"myparam" => "myvalue"} , {"X-Some-Header" => "MyValue"}
I get the following error:
Failure/Error: post api_v1_post_path(#myid), {"myparam" =>"myvalue"}, headers
ActionDispatch::ParamsParser::ParseError:
795: unexpected token at 'myparam'
Any ideas?
It seems that the POST params are expected to be JSON encoded. 795: unexpected token at 'myparam' is caused when the app tries to JSON decode the params that are not encoded.
Use .to_json with the post params.
post api_v1_post_path(#myid), {"myparam" => "myvalue"}.to_json , {"X-Some-Header" => "MyValue"}
You may want to use let:
describe 'Test' do
let( :params ){{ myparam: 'myvalue' }}
let( :headers ){{ 'X-Some-Header' => 'MyValue' }}
it 'succeeds' do
post api_v1_post_path(#myid), params.to_json , headers