From: Darshana Gangwal on
Hello,

I am trying to post the form (http) from the below html "test.html" and
am using the below ruby code "submit_test.rb" to submit the form but
getting html response page with message
"Unhandled Request: The server is not setup to handle this type of
Request:" instead of search result from keyword "technology".
can anyone give any pointer to get the proper search result?

test.html
--------------------------------------------------------------------
<html>
<body> <h2>Submit This!!!</h2>
<form action =
"http://www.chamberdata.cc/ccao/wc.dll?Mem~KeywordList~&Org=txallen"
method = "post">
<input type = "text" value = "" name = "Keyword">
<button type = "submit" value = "submit" name = "click me">
</form>
</body>
</html>
--------------------------------------------------------------------

submit_test.rb
--------------------------------------------------------------------
require 'rubygems'
require 'nokogiri'
require 'uri'
require 'net/http'

def submit_form
form_map = Hash.new
file_name = "test.html"
html_content = get_file_content(file_name)
url = extract_url_str(html_content)
form_map['Keyword'] = "technology" # keyword to be submitted
form_map['click me'] = "submit"

# submit form
response = Net::HTTP.post_form(URI.parse(url), form_map)
response_body = response.body if Net::HTTPSuccess
p "response_body #{response_body}"
end

def extract_url_str(response_body)
url_str = ""
doc = Nokogiri::HTML(response_body)
form_array = doc.search("form")
if form_array.nil? then
raise "form_array == nil"
end

form_array.each { |form|
if form.get_attribute("action").include?("KeywordList") then
url_str = form.get_attribute("action")
break
end
}
return url_str

end

def get_file_content(file_path)
file = File.open(file_path,"r")
html_response = file.read
return html_response
end

submit_form
--------------------------------------------------------------------
--
Posted via http://www.ruby-forum.com/.