[LRUG] submitting a form programatically

Dan Webb dan at danwebb.net
Fri Feb 15 08:46:03 PST 2008


Hi Ed,

Net::HTTP::Post is what you are after:

require 'net/http'
    require 'uri'

    #1: Simple POST
    res = Net::HTTP.post_form(URI.parse('http://www.example.com/search.cgi'),
                              {'q'=>'ruby', 'max'=>'50'})
    puts res.body

    #2: POST with basic authentication
    res = Net::HTTP.post_form(URI.parse('http://jack:pass@www.example.com/todo.cgi'),
                                        {'from'=>'2005-01-01',
'to'=>'2005-03-31'})
    puts res.body

    #3: Detailed control
    url = URI.parse('http://www.example.com/todo.cgi')
    req = Net::HTTP::Post.new(url.path)
    req.basic_auth 'jack', 'pass'
    req.set_form_data({'from'=>'2005-01-01', 'to'=>'2005-03-31'}, ';')
    res = Net::HTTP.new(url.host, url.port).start {|http| http.request(req) }
    case res
    when Net::HTTPSuccess, Net::HTTPRedirection
      # OK
    else
      res.error!
    end

Cheers,

Dan

On 2/15/08, Ed Davey <Ed at veryreal.co.uk> wrote:
> What's a good (simple) way of submitting a form programatically from
>  inside a controller and then returning the response to a template?
>
>  (I'm sending shopping basket data through to Worldpay and getting
>  back their form for the customer to submit their credit card)
>
>  I saw a rubyquiz solution using Mechanize which looked interesting.
>  Also came across ruby-htmlform which can use open-uri or net/http
>
>  Any tips?
>
>  Thanks
>
>  ED
>  _______________________________________________
>  Chat mailing list
>  Chat at lists.lrug.org
>  http://lists.lrug.org/listinfo.cgi/chat-lrug.org
>


-- 
Dan Webb
http://www.danwebb.net

aim: danwrong123
skype: danwrong



More information about the Chat mailing list