How to post form data using Ruby

Source: Internet
Author: User

From http://biodegradablegeek.com/2008/04/how-to-post-form-data-using-ruby/

PostIng data on web forms is essential for writing tools and services that interact with resources already available on the web. you can grab information from your Gmail account, add a new thread to a forum from your own app, etc.

The following is a brief example on how this can be done in Ruby using net: httpand this post form example.

Looking at the source (interlacken.com/webdbdev/ch05/formpost.asp ):

View Source
<form method="POST" action="formpost.asp"><p><input type="text" name="box1″ size="20″ value=""><input type="submit" value="Submit" name="button1″></p></form>
We see two attributes are sent to the formpost. asp script when the user hits the submit button: A textbox named Box1And the value of the submit button, named Submit. If this form used a get method, we wocould just fetch the URL postfixed with (for example) ? Box1 = Our + TEXT + here. Fortunately, Ruby's net: HTTP makes posting data just as easy.

The ruby code:

View Source
#!/usr/bin/rubyrequire "uri"require "net/http"params = {'box1′ => 'Nothing is less important than which fork you use. Etiquette is the science of living. It embraces everything. It is ethics. It is honor. -Emily Post','button1′ => 'Submit'}x = Net::HTTP.post_form(URI.parse('http://www.interlacken.com/webdbdev/ch05/formpost.asp'), params)puts x.body# Uncomment this if you want output in a file# File.open('out.htm', 'w') { |f| f.write x.body }
Sending the value of button1 is optional in this case, but sometimes this value is checked in the server side script. one example is when the coder wants to find out if the form has been submitted-as opposed to it being the user's first visit to the form-without creating a hidden attribute to send along w/the other form fields. besides, there's no harm in sending a few more bytes.

If you're curious about Uri. parse, it simply makes the URI easier to work with by separating and classifying each of its attributes, inclutively letting the methods in net: HTTP do their sole job only, instead of having to analyze and parse the URL. more info on this in the ruby Doc.

Assuming no errors, running this example (Ruby postpostOrChmod A + x postpost. RB;./postpost. Rb) Yields:

View Source
<form method="POST" action="formpost.asp"><p><input type="text" name="box1″ size="20″ value="NOTHING IS LESSIMPORTANT THAN WHICH FORK YOU USE. ETIQUETTE IS THESCIENCE OF LIVING. IT EMBRACES EVERYTHING. IT IS ETHICS.IT IS HONOR. -EMILY POST"><input type="submit" value="Submit" name="button1″></p></form>
In practice, you might want to use a more specialized library to handle what you're doing. Be sure to check out mechanic and rest-client.

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.