How to POST Form Data Using Ruby(转)
2011-05-05 21:19
369 查看
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
We see two attributes are sent to the formpost.asp script when the user hits the submit button: A textbox named box1 and the value of the submit button, named Submit. If this form used a GET method, we would 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
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, effectively 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 postpost or chmod a+x postpost.rb; ./postpost.rb) yields:
view source
In practice, you might want to use a more specialized library to handle what you’re doing. Be sure to check out Mechanize and Rest-client.
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 box1 and the value of the submit button, named Submit. If this form used a GET method, we would 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/ruby require "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, effectively 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 postpost or chmod 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 LESS IMPORTANT THAN WHICH FORK YOU USE. ETIQUETTE IS THE SCIENCE 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 Mechanize and Rest-client.
相关文章推荐
- How to post a form using fetch in react native
- How to simulate a Form POST request by using WinInet
- How to simulate a Form POST request by using WinInet
- How To Submit Form Data by Using XMLHTTP or ServerXMLHTTP Object
- Http client to POST using multipart/form-data
- Http client to POST using multipart/form-data
- How to simulate a Form POST request by using WinInet
- How to use JSon data in mvc action and post form data use JQuery ajax
- How To Automate Internet Explorer to POST Form Data
- [转] How to import a large data set using XPO efficiently within a transaction
- [Project] Simulate HTTP Post Request to obtain data from Web Page by using Python Scrapy Framework
- Big Data Counting: How to count a billion distinct objects using only 1.5KB of Memory
- How to copy a datafile from ASM to a file system not using RMAN
- How to change the background color of form using C++ - 如何用C++改变窗体的颜色
- how to select data in multiple datatables after using join,group by and order by in Linq
- ASP.NET how to send data with post method?
- Android Tutorial: How To Post Data From An Android App To a Website
- How to populate the datagrid on background thread with data binding by using Visual C#
- How To Read and Write BLOB Data by Using ADO.NET with Visual C# .NET
- [Project] Simulate HTTP Post Request to obtain data from Web Page by using Python Scrapy Framework