Rails http request - don't wait for response - ruby-on-rails

I am coding in Ruby-on-Rails
I would like to send a http request to another service but not wait for a response.
Pseudocode:
def notification
require 'net/http'
...
# send net/http request
Net::HTTP.post_form(url, params)
render :text => "Rendered quickly as did not wait for response from POST"
end
Is there any way to send the POST request and not wait for a response and just to quickly render the page?

You can try delayed_job. It is mainly used to run processes in background. Once you install delayed_job you can do like this.
require 'net/http'
def notification
...
your_http_request #calling method
render :text => "Rendered quickly as did not wait for response from POST"
end
def your_http_request
# send net/http request
Net::HTTP.post_form(url, params)
#do your stuff like where to save response
end
handle_asynchronously :your_http_request

Related

timeout handling in rails api client

I 'm trying to build a Rails API client. There is an api where I can receive my data as json, which works great so far.
Now I am trying to do some timeout handling but I don't know how. I mean literally. How should I even use timeout handling?
I saw something in a tutorial which I translated for my used gem "net/http" but I cannot imagine that this has even any effect.
Here is my controller code:
require 'net/http'
class OverviewController < ApplicationController
def api_key
ENV["API_KEY"]
end
def handle_timeouts
begin
yield
rescue Net::OpenTimeout, Net::ReadTimeout
{}
end
end
def index
handle_timeouts do
url = "https://example.com/api/#{ api_key }"
uri = URI(url)
response = Net::HTTP.get(uri)
#url_debug = url
#my_hash = response
end
end
end

Saving rails request to JSON/YML

I'm using service which sends Webhooks to my application. I want to write RSpec test for handling them. It's important to have this request exactly the same (remote caller IP, headers with encrypted content).
I tried to save request as json:
class WebhookController < ApplicationController
def some_callback
File.open('temp/request_example.json','w') do |f|
f.write request.to_json
end
end
end
so I could later do:
describe WebhookController do
subject { get :some_callback, JSON.parse(File.open('temp/request_example.json')) }
it 'does something' do;end
end
but unfortunately you cannot call request.to_json(request.to_json
IOError: not opened for reading). You can't either get directly to request.body or request.headers.
How to save such request for later usage in tests? Is there any gem for it?

How to print response body to stdout/stderr on Rails

I would like to print out response body generated by my app to stdout/stderr for debugging purposes. The traffic is server-server so I cannot use client tools to get hold of http traffic.
There is a mention of puts #response.body in http://api.rubyonrails.org/classes/ActionDispatch/Response.html, however in my app controller #response is undefined. Is there a way for me to print response body to logs in my rails app, and if so, how?
Based on the answer given, did it like this:
after_filter :print_response_body, :only => [:index]
def print_response_body
$stderr.puts response.body
end
In your controller, try
after_filter do
puts response.body
end

Ruby - Get request body from incoming http call

I am receiving http requests to my rails application to a url /account/postback
The body of this incoming request contains some json that I need to retrieve, how can I do this in ruby?
The following should print the body of the request
routes.rb
map.connect 'account/:action', :controller => 'accounts'
accounts_controller.rb
class AccountsController < ApplicationController
def postback
puts request.body.read
end
end
If your HTTP call is using the POST verb you could alternatively use request.raw_post to retrieve the contents sent in the request's body.
Hope it helps!

How do I render two html documents per action?

After user push save button, I need to render new page and render_to_string preview of this page in same time. To store it into DB.
So i got DoubleRenderError exception.
I try to stub #performed?
But Layouts purging after first render. Any ideas?
Thank you for answers!
I've successfully used both render_to_string and render on the same request.
I think you need to make sure you call render_to_string first. YMMV
I would probably do this using rack middleware.
class ResponseLoggerMiddleware
def initialize(app)
#app = app
end
def call(env)
status, headers, response = #app.call(env)
... save your response to the database ...
[status, headers, response]
end
end
You can install it like this:
# environment.rb
Rails::Initializer.run do |config|
...
config.middleware.use ResponseLoggerMiddleware
end

Resources