开发者

How do I read POST info in Rails?

开发者 https://www.devze.com 2023-02-05 09:23 出处:网络
I would like to know how to both send and receive info through HTTP POST requests. I\'m using Ruby v.1.8.7 and rails v.2.3.8.

I would like to know how to both send and receive info through HTTP POST requests.

I'm using Ruby v.1.8.7 and rails v.2.3.8.

EDIT:

My action's code is the following:

require 'net/http'

# get the url that we need to post to
url = URI.parse('http://localhost:3000/ipn/payments')

xml_notificaciones = "<NOTIFICACION><TIPONOTIFICACION>1</TIPONOTIFICACION><OPERACIONES><OPERACION><TIPO>1</TIPO><ID>31548</ID></OPERACION><OPERACION><TIPO>1</TIPO><ID>XA5547</ID></OPEARACI开发者_如何学JAVAON></OPERACIONES></NOTIFICACION>"

# build the params string
post_args1 = { 'NOTIFICACION' => xml_notificaciones }
# send the request
resp, data = Net::HTTP.post_form(url, post_args1)

When I execute the above code the page will load for a while until I get execution expired message.

I actually have a routes entry for http://localhost:3000/ipn/payments url, and is the following:

map.received_ipn_payments '/ipn/payments', :controller => 'payments', :action => 'parse_received_data', :method => :post

I have a raise params.inspect so I know when it works, but nothing so far...


Are you trying to post to the same rails application from one of your controllers? If you are running this on a single webrick/mongrel instance, you can only handle one single request. So, if you make a request to your app from your browser, and this request needs to make another request to the same application (which blocks the first one), it'll get stuck and just time out.

I'm not sure why you need your controller to do a second request, it seems to me that you need to run code in another controller/action. You could either redirect to that other controller/action, but that would need a GET request, or move your code from the controller to a model or module and call the appropriate methods from both your controller actions.

Edit: If you are trying to test your payment processor integration you should be using their testing environment (provided they have one) instead of emulating it in your application. If you really need to write code to emulate the payment process, you should do that in a different application running on its own webrick/mongrel instance. Then you'll be able to make requests to that other application.


To receive the requests into your application, set up a route in config/routes.rb for it. See the Routing Guide for more information. Route it to a controller and then to access the information sent through use the params method.

As for sending back data... an alternative to andrea's answer, you could use the Typhoeus gem to make the HTTP requests. It's got a really neat syntax to it and I've been using it recently with no worries.


to receiving a post request you have only to create an action in a controller and set the routes with method post.

to send a request you could use something similar

require 'net/http'
# get the url that we need to post to
url = URI.parse('http://www.url.com/subscribe')# build the params string
post_args1 = { 'email' => params[:email] }
# send the request
resp, data = Net::HTTP.post_form(url, post_args1)
0

精彩评论

暂无评论...
验证码 换一张
取 消