melodrama
melodrama

Reputation: 265

Make a POST of form in Ruby

I want to make a post request with the Net::HTTP in Ruby. The website is like:

 <form method="post" action="login.php">
    <tr><td><input type="text" name="id" /></td></tr>
    <tr><td><input type="password" name="password" /></td>
    <td> <input type="submit" name = "submit" value="Login" /> </td></tr>
 </form>

Following the example in Net::HTTP in Ruby, the ruby script is like:

 uri = URI('http://www.example.com/login.php')
 req = Net::HTTP::Post.new(uri)
 req.set_form_data('id' => 'myid', 'password' => 'mypassword', 'submit' => 'Login') 
 res = Net::HTTP.start(uri.hostname, uri.port) do |http|
     http.request(req)
 end

I assume res should be the page after login in, but the login is not sucessful. What is the problem?

Upvotes: 0

Views: 120

Answers (1)

Amadan
Amadan

Reputation: 198324

What do you mean by "the login is not successful"? Does the response tell you "invalid password" or something? Or is the response as you think it should be, but you expect a subsequent request to be "logged in", but it isn't? In the latter case, do you carry the cookie to the next request? Maybe you should consider Mechanize instead.

Upvotes: 1

Related Questions