如何在 Sinatra 代码中发出 POST 请求?

问题描述:

单击表单中的按钮将发送一个 POST 请求,由以下代码段处理.

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

如何从 Ruby 代码中向其他网站发送另一个 POST 请求?

How can I send another POST request to a different website from within the Ruby code?

使用 Net::HTTP 来自 Ruby 标准库或 HTTParty gem.

Use Net::HTTP from the Ruby Standard Library or the HTTParty gem.