# How to POST Form Data?

**URL:** <https://www.ruby-forum.com/t/how-to-post-form-data/191094>\
**Category:** Ruby\
**Created:** [July 5, 2010, 10:32am UTC](https://www.ruby-forum.com/t/how-to-post-form-data/191094 "2010-07-05T10:32:39Z")\
**Posts on this page:** 1\
**Page:** 1

<div class="post-metadata">

**Author:** ![darshana](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/d/53a042/32.png) [@darshana](https://www.ruby-forum.com/u/darshana)\
**Post date:** [July 5, 2010, 10:32am UTC](https://www.ruby-forum.com/t/how-to-post-form-data/191094/1 "2010-07-05T10:32:39Z")

</div>

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

## Submit This!!!
--------------------------------------------------------------------
## 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
