RestClient::RequestTimeout in controller#method ruby on rails - ruby-on-rails

I am making the following rest get request:
rest_user = JSON.parse(RestClient.get APP_CONFIG['api_url'] + "users/", :params => {:token => APP_CONFIG['token'], :full => 'true'} )["users"]
I get the following error back after a minute:
RestClient::RequestTimeout in controller#method
rbenv/versions/1.8.7-p374/lib/ruby/gems/1.8/gems/rest-client-1.6.7/lib/restclient/request.rb:184:in `transmit'
rbenv/versions/1.8.7-p374/lib/ruby/gems/1.8/gems/rest-client-1.6.7/lib/restclient/request.rb:64:in `execute'
rbenv/versions/1.8.7-p374/lib/ruby/gems/1.8/gems/rest-client-1.6.7/lib/restclient/request.rb:33:in `execute'
rbenv/versions/1.8.7-p374/lib/ruby/gems/1.8/gems/rest-client-1.6.7/lib/restclient.rb:68:in `get'
Now when I am tailing the logs of the actual service that is receiving this request, it is performing the requested request and completes it without any error.
But the consumer(the web applicaion) is throwing the request timeout error before the request has actually been fully completed.

It is absolutely normal. Your web app raises an error, because RestClient raises an error after 60 seconds if it has not received response yet.
The app you are calling has no knowledge of that fact, so it continues processing as long as it is required to complete the request (it may take, 61 seconds, 70, ... ).
url = APP_CONFIG['api_url'] + "users/"
payload = {:token => APP_CONFIG['token'], :full => 'true'}
RestClient::Execute.execute(method: :get, url: url, payload: payload, timeout: 120)

Related

wrong number of arguments (given 2, expected 0..1) with Faraday post request

I'm trying to send http post request from my application using faraday gem, but I keep receiving this error wrong number of arguments (given 2, expected 0..1) in that line response = connection.post '/submissions' do |request|
and here is my code
def post_submission
connection = Faraday.new(url: URL, headers: { 'Content-Type' => 'application/json' })
response = connection.post '/submissions' do |request|
request.params = { base64_encoded: false, wait: true }
request.body = JSON.generate(
source_code: #params[:source_code],
stdin: #params[:input],
expected_output: #problem.output,
anguage_id: #programming_languge.judge_code
)
end
end
No matter what updates I do to the code, I keep receiving the same error, even when I don't add any arguments! I tried every way in the documentation to create the connection and the request but all give me the same error, anyone could help, please?
Thanks in advance
UPDATE: Screen from the error

Rails RestClient POST request failing with "400 Bad Request"

Looking at the docs there aren't any good examples of how to make a POST request. I need to make a POST request with a auth_token parameter and get a response back:
response = RestClient::Request.execute(method: :post,
url: 'http://api.example.com/starthere',
payload: '{"auth_token" : "my_token"}',
headers: {"Content-Type" => "text/plain"}
)
400 bad request error:
RestClient::BadRequest: 400 Bad Request
from /Users/me/.rbenv/versions/2.2.3/lib/ruby/gems/2.2.0/gems/rest-client-1.8.0/lib/restclient/abstract_response.rb:74:in `return!'
from /Users/me/.rbenv/versions/2.2.3/lib/ruby/gems/2.2.0/gems/rest-client-1.8.0/lib/restclient/request.rb:495:in `process_result'
from /Users/me/.rbenv/versions/2.2.3/lib/ruby/gems/2.2.0/gems/rest-client-1.8.0/lib/me/request.rb:421:in `block in transmit'
Any good examples how to make a POST request using RestClient?
EDIT:
This is how I make the request in the model:
def start
response = RestClient::Request.execute(method: :post,
url: 'http://api.example.com/starthere',
payload: '{"auth_token" : "my_token"}',
headers: {"Content-Type" => "text/plain"}
)
puts response
end
Try using a hash like this:
def start
url= 'http://api.example.com/starthere'
params = {auth_token: 'my_token'}.to_json
response = RestClient.post url, params
puts response
end
If you just want to replicate the curl request:
response = RestClient::Request.execute(method: :post, url: 'http://api.example.com/starthere', payload: {"auth_token" => "my_token"})
Both Curl and RestClient defaults to the same content type (application/x-www-form-urlencoded) when posting data the this format.
In case you land here having the same Issue, Just know that this is a common error that happens when your environment variables are not "set".
I put this in quotes because you might have set it but not available in the current terminal session!
You can check if the ENV KEY is available with:
printenv <yourenvkey>
if you get nothing then it means you need to re-add it or just put it in your bash files
FYI: Putting my ENV variables in my ~/.bash_profile fixed it

bad URI(is not URI?): {"message":"d64","type":"success"}

Am using httparty to send API request, I want to get output response after i sent request,
url = HTTParty.post("https://example.com/api/sendhttp.php",
:query => { :authkey => "authkeyvalue",
:mobiles => mobileNos,
:message => messages,
:sender => "senderid",
:route => "routeid",
:response => 'json'
})
response = HTTParty.get(url)
puts response.body, response.code, response.message, response.headers.inspect
But when i run above code it throws bad URI(is not URI?): {"message":"d64","type":"success"} error. How to solve it and get the response?
This looks like you are running a POST to https://example.com/api/sendhttp.php with JSON format, this succeeds, and you're getting a response of:
{"message":"d64","type":"success"}
So your url variable now contains {"message":"d64","type":"success"}, which is clearly not a valid URL, so when you try to do a GET on it, you get an error. You've already got a response from the first POST, you should perhaps parse this? You don't need to do
response = HTTPParty.get(url)
unless you're expecting a second GET request to a URL which is returned by the first.

How to handle RestClient::ServerBrokeConnection

I am using the latest version of rest-client gem and upon external access I see a lots of RestClient::ServerBrokeConnection errors, how should I handle this?
The following call fails
response = RestClient::Request.execute(method: :post, url: url, headers: headers, "Content-Type" => "application/x-www-form-urlencoded")
This error happens when the server broke the connection with the client. You can decide to retry the request or just bubble the error for the user to know about it and handle it.
Because how rest-client handles broken connections as shown here, all you can do is rescue from it
begin
response = RestClient::Request.execute(method: :post, url: url, headers: headers, "Content-Type" => "application/x-www-form-urlencoded")
rescue RestClient::ServerBrokeConnection
// retry or do something
end

Google Drive API watch 400 error Ruby on Rails

I'm implementing the google drive api using the OAuth2 gem (ruby on rails). I'm not using the client library because I'm also integrating other API's, so I'm trying to make all these calls as modular as possible. I'm having trouble with the this request: POST https://www.googleapis.com/drive/v2/changes/watch.
I keep getting this error:
{"errors"=>[{"domain"=>"global", "reason"=>"required", "message"=>"entity.resource"}],
"code"=>400, "message"=>"entity.resource"}: { "error": { "errors": [ { "domain":
"global", "reason": "required", "message": "entity.resource" } ], "code": 400,
"message": "entity.resource" } }
which is not very useful. It may not be Google. It could be OAuth2, but I don't think so, because the debugger gets to the response after making the connection. Well, at this point, I don't know anything, so any help is appreciated. There is THIS GUY who has exactly the same error code as me, as well as the same conclusion.
Anyway, the relevant code parts:
First, the parameters I pass to OAuth2::AccessToken's post method(I need more than 10 rep to post another link, but here is the dochttp://rdoc.info/github/intridea/oauth2/ebe4be038ec14b349682/OAuth2/AccessToken#post-instance_method)(you can click on the request method to see how the params are handled)
base_url = request.protocol + request.host_with_port
channel_id = (0...50).map { ('a'..'z').to_a[rand(26)] }.join
body_post = {:id => channel_id,:type => 'web_hook',:address => base_url + "/googledrive/webhook"}
headers = {'Content-Type' => 'application/json'}
response = makeApiCall(token,"google_drive","/changes/watch","post",{},body_post,headers)
All this does is built my request with my the appropriate request body and headers for the call
Here is the relevant part from makeApiCall (at the line token.post is where the request is made, and where it breaks)
params = {"oauth_consumer_key" => ENV[key], "access_token" => token.token}.merge(params)
#body = Rack::Utils.build_query(body)
opts = {
:params => params,
:body => body,
:headers => headers
}
if(method=="get")
response = token.get(base + path,:opts => opts)
elsif(method=="post")
debugger
response = token.post(base + path,:opts => opts)
end
This is my first or second post, so forgive me if I messed anything up.
Okay, after struggling for so long, I figured out why it wasn't working. I forgot to do this:
JSON.generate(body_post)
So that my body was actually in application/json form.
Also, this line:
response = token.post(base + path,:opts => opts)
should be this:
response = token.post(base + path,opts)
simply because I'm not assigning the opts key, but the variable that I'm passing. Silly me.

Resources