Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to make a POST request inside Sinatra code?

Tags:

ruby

sinatra

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?

like image 262
Takkun Avatar asked Apr 17 '12 03:04

Takkun


1 Answers

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

like image 72
Jordan Running Avatar answered Nov 15 '22 07:11

Jordan Running