Dmitriy Polovinkin Dmitriy Polovinkin - 2 months ago 11
Ruby Question

how send post request and open url in Ruby Sinatra

i am sending a post request,
and need to get result like with html form,
where we going to some url after doing request

<form action="url" method="post">


i tried this:

return response.body()


and i get only body of html page,
but i need to redirect to this page.
what right way to go to needed URL?

here my code for post request:

post '/send' do
uri = URI.parse("https://wl.walletone.com/checkout/checkout/Index")
response = Net::HTTP.post_form(uri, {
"WMI_MERCHANT_ID" => "xxx",
"WMI_PAYMENT_AMOUNT" => "10.00",
"WMI_CURRENCY_ID" => "643"
})
end

Answer

If you want to directly pass along a POST request and redirect to it, you can do

post '/send' do
  redirect <your_url>, 307
end

307 is the HTTP status code to not modify the request method.

Handle a redirect response like this:

post '/send' do
  # post form
  redirect response['location'], 302
end