How to POST Form Data?

Discussion in 'Ruby' started by Darshana Gangwal, Jul 5, 2010.

  1. 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/.
    Darshana Gangwal, Jul 5, 2010
    #1
    1. Advertising

Want to reply to this thread or ask your own question?

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. Vishal
    Replies:
    1
    Views:
    654
    Steve C. Orr [MVP, MCSD]
    Dec 21, 2004
  2. Replies:
    2
    Views:
    716
  3. Don Glover the younger

    ASP, FORMS, POST METHOD And Post with out form(???)

    Don Glover the younger, Jul 13, 2003, in forum: ASP General
    Replies:
    0
    Views:
    439
    Don Glover the younger
    Jul 13, 2003
  4. J. Muenchbourg
    Replies:
    0
    Views:
    348
    J. Muenchbourg
    Aug 6, 2003
  5. Adam
    Replies:
    8
    Views:
    403
Loading...

Share This Page