Not sure why I keep getting 404 error with Ruby Rest Client - ruby

I'm using the following code:
RestClient.get "https://myurl.com/apps/v1/company/apps/appname/device/#{device_uuid}", :params => {:client_id => client_id, :client_secret => client_secret} do |response, request, result, &block|
if [404].include? response.code
puts 'ERROR' + response.body
else
response.return!(request, result, &block)
end
end
I am trying to use client_id and client_secret as query string parameters, and I know that when I manually do a get on this url in my browser that it is valid - however when I try to use this rest client get request, I only seem to be getting a 404 resource not found back.
The end result I am trying to do is to get the JSON back from this get request as well - it may need to be a separate question but I am also having issues with getting the JSON contents from the response body.
Thank you for any help

The code from above worked better as:
response = RestClient.get "https://myurl.com/apps/v1/company/apps/app1/devices/#device_uuid}",
{:params => {:client_id => client_id, :client_secret => client_secret}, :accept => :json}
It was not just adding the :accept => :json but also making sure it was passed along as part of my parameters - no idea why it bubbled up as a 404 though.

Related

Capture HTTP call response using Chef

I'm currently using Chef to build out a cookbook that has to fire off a bunch of POST calls to this API and I have to capture the response in a variable to use it in a second HTTP call.
I've tried using the http_request resource from Ruby but I can only fire the call but don't know how to get the response captured:
http_request 'authorize' do
action :post
url '*****************************' headers ({
'Content-Type' => 'application/json'
}) message ({
:Username => "**********",
:Password => "**********"
}).to_json
end
In another attempt, I tried using Chef's http client to fire off a POST call and a get a response:
require "net/https"
require "uri"
require "json"
uri = URI("******************************")
req = Net::HTTP::Post.new(uri)
req.set_form_data("Username" => "********", "Password" => "*********")
res = Net::HTTP.start(uri.hostname, uri.port) do |http|
http.request(req)
end
case res
when Net::HTTPSuccess, Net::HTTPRedirection
# OK
else
res.value
end
But I keep getting this error when I run the chef-client on my node:
EOFError
--------
end of file reached
How can I send off a POST call using Chef/Ruby and capture its response?
You want to use the Chef::HTTP client class, see https://coderanger.net/chef-tips/#4 for an example.
In my case the request url had https so I had to add this line and it was working.
http.use_ssl = true
If you want a more generic approach that can handle both http and https see this answer -
https://stackoverflow.com/a/9227933/6226283

In ruby with sinatra, How to get I response with get method on rest client?

I use ruby with sinatra and I used rest-client on import for payment.
I got token that string typed through post method on specific url: '... /users/getToken'.
Using this token, I wanna get payments information with get method on this url:
get_url = 'https://api/iamport.kr/payments/'+imp_uid
the detail codes are below,
def get_paymentsdetails(token, imp_uid)
get_url = 'https://api.iamport.kr/payments/'+imp_uid
response = RestClient.get get_url, :data => {}.to_json, :accept => :json, :headers => {'Authorization' => token}
json = JSON.parse(response, :symbolize_names => true)
# json = JSON.parse(response.to_json, {:symbolize_names => true})
return json
end
However, I got 401 unauthorized error on this part of code.
response = RestClient.get get_url, :data => {}.to_json, :accept => :json, :headers => {'Authorization' => token}
After I access get_url with specific imp_uid, I got this page,{"code":-1,"message":"Unauthorized","response":null}
I checked parameter token and imp_uid of get_paymentsdetails function have valid string values,, so How can I access response parameter??
I think that there are some problems on response = RestClient.get get_url.... code.
Thanks.
Method 'get' from the 'RestClient' class return some object with attributes. So response have few values. Which of them do you need? Access to them you can get by their names, its described here.
In your case, after response = RestClient.get get_url... you should have variable response and ability to call response.headers, response.code or response.body.
But im afraid that you have some problems with autorization, which means that imp_uid or token is not correct. Thats why remote server sended to you responce with http-code 401 (Unauthorized). If it is so you should try to check your imp_uid and token. If everything is correct try to reach support of iamport.kr .

Net:HTTP timeout with Post class

I am trying to debug a web services issue, and have come across an interesting issue with Net:HTTP behaviour (no doubt my failure to understand how the Net::HTTP library should be used).
If I use the following, I get a response back from the server
require 'net/http'
USER='me'
PASS='me'
uri=URI('https://web.server.net/GetXMLData/webviewctl.asmx')
res=Net::HTTP.post_form(uri, 'action' => 'GetMyAssets', 'User' => USER, 'Password' => PASS)
Alternatively I could use an instantiated object of class Net::HTTP::Post rather than the class method - so the final line becomes:
req = Net::HTTP::Post.new(uri)
req.set_form_data('action' => 'GetMyAssets', 'User' => USER, 'Password' => PASS)
res = Net::HTTP.start(uri.hostname, uri.port) do |http|
http.request(req)
end
However, this generates a read timeout: /home/steve/.rvm/rubies/ruby-2.0.0-p195/lib/ruby/2.0.0/net/protocol.rb:158:in 'rescue in rbuf_fill': Net::ReadTimeout (Net::ReadTimeout)
Does anyone have any thoughts on why this might be? What am I doing wrong in the second method. I'd prefer to use this as I can get finer control on what is actually sent to the server.
Thanks in advance
Steve

400 Bad Request Nestful Ruby

I'm trying to use the Pocket API to authorize my application. So I'm using Nestful to send HTTP requests. And everytime I try sending a request I get a 400 Bad Request. The Pocket documentation says that it could be that it's either a missing consumer key or a missing redirect url.
But now I'm looking at the network tab in Chrome and it says that there is a 500 Internal Service Error. What are these things, and how can I fix them?
My code:
require "nestful"
require "sinatra"
require "uri"
get '/' do
params = {
:consumer_key => '******************************',
:redirect_uri => 'http://localhost:4567/callback'
}
response = Nestful.post 'https://www.getpocket.com/v3/oauth/request',
:params => params,
:format => :json
response.body
response.headers
end
get '/callback' do
"hello world"
end
So I got help on my problem. It turns out that params was already a hash, and so I did not need to say :params => params because that would be redundant.
Before
response = Nestful.post 'https://www.getpocket.com/v3/oauth/request',
:params => params,
:format => :json
After
response = Nestful.post 'https://getpocket.com/v3/oauth/request',
params,
:format => :json

Only getting response codes, but not actual response, from OAuth::AccessToken

In my User model, I have the following code:
def twitter_client
OAuth::AccessToken.new(twitter_oauth, self.access_token_key, self.access_token_secret)
end
def twitter_oauth
OAuth::Consumer.new(Twitter::Login.consumer_key, Twitter::Login.secret, :site => Twitter::Login.site)
end
This way I can call User#twitter_client like so:
current_user.twitter_client.post('/statuses/update.xml', {'status' => 'foooo', 'Accept' => 'application/xml'})
This works well, and actually does update the status, and returns the following:
#<Net::HTTPOK 200 OK readbody=true>
This really is not a problem for updating statuses. However, when I want to get the latest tweets, all I get is a response code object, without the actual content from the response:
current_user.twitter_client.get('/statuses/user_timeline.xml', {'Accept' => 'application/xml'})
=> #<Net::HTTPOK 200 OK readbody=true>
This returned object is just an instance of Net::HTTPOK and contains no tweet data.
How do I get the tweet data?
How about...?
res = current_user.twitter_client.get('/statuses/user_timeline.xml', {'Accept' => 'application/xml'})
puts res.body

Resources