问题
Clicking a button in a form will send a POST request to be handled by the following piece of code.
post '/register' do
#send post request to http://www.randomsite.com
#parse response
#do something with it
@user = User.first(:name => params['regUsername'])
if @user == nil
@user = User.create(
:name => params['regUsername'],
:pass => Password.create(params['regPassword']),
:email => params['regEmail'],
:created_date => Time.now
)
redirect '/'
else
"User already exists."
end
end
How can I send another POST request to a different website from within the Ruby code?
回答1:
Use Net::HTTP from the Ruby Standard Library or the HTTParty gem.
来源:https://stackoverflow.com/questions/10184737/how-to-make-a-post-request-inside-sinatra-code