2

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?

Harry
  • 1,659
  • 5
  • 19
  • 34
  • You can move the request into a background worker. Check out http://railscasts.com/?tag_id=32 for a bunch of different options for accomplishing this – Matthew Ertel Mar 01 '13 at 18:17

1 Answers1

1

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
Rahul Tapali
  • 9,887
  • 7
  • 31
  • 44