posting form data in ruby 1.8.2?

Discussion in 'Ruby' started by 7stud --, Sep 16, 2007.

  1. 7stud --

    7stud -- Guest

    Hi,

    I'm trying to figure out how to post form data to a url, but the methods
    used in the examples in the Net::HTTP docs use methods that aren't
    available in my ruby install:


    Posting Form Data:

    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:p/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



    I looked at the source code for the methods that cause missing method
    errors, and I tried to use the lower level methods used in the source,
    but some of them weren't available either, which required that I look
    into more source files, and eventually I got lost in a labyrinth. Here
    are my feeble beginnings:

    require "net/http"
    require "uri"

    url = URI.parse("https://login.yahoo.com/config/login?")
    puts url.path
    puts url.port

    req = Net::HTTP::post.new(url.path)
    #req.content_type = 'application/x-www-form-urlencoded'
    #req.body = "username=tailing_loop2003&passwd=bwopmd"
    #I get an error saying there is no content_type= method.

    #response = Net::HTTP.post_form("https://login.yahoo.com/config/login?",
    {"username", "aaaaaa", "passwd", "bbbb"})
    #No post_form method.


    Is there an example online that I can look at somewhere? I can't find
    anything in pickaxe2 or ruby way2.
    --
    Posted via http://www.ruby-forum.com/.
    7stud --, Sep 16, 2007
    #1
    1. Advertising

  2. 7stud --

    Bil Kleb Guest

    Bil Kleb, Sep 16, 2007
    #2
    1. Advertising


  3. > #req.content_type = 'application/x-www-form-urlencoded'


    > #I get an error saying there is no content_type= method.
    >
    > #response = Net::HTTP.post_form("https://login.yahoo.com/config/login?",
    > {"username", "aaaaaa", "passwd", "bbbb"})
    > #No post_form method.
    >
    >

    content_type should be in () IE: content_type=(type, params = {})
    instead of quotes

    same type of scenario for no post_form method

    Net::HTTP::post_form(url, params)

    :) have fun!
    --
    Posted via http://www.ruby-forum.com/.
    Michael Linfield, Sep 17, 2007
    #3
    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. Jeff Baker

    WebClient form Posting with form name?

    Jeff Baker, Aug 28, 2003, in forum: ASP .Net
    Replies:
    2
    Views:
    747
    John Saunders
    Aug 29, 2003
  2. alanb
    Replies:
    2
    Views:
    520
    alanb
    Apr 23, 2004
  3. Rob Meade
    Replies:
    0
    Views:
    421
    Rob Meade
    Dec 1, 2004
  4. AmitKu
    Replies:
    7
    Views:
    499
    John Timney \(MVP\)
    Jan 8, 2007
  5. Replies:
    3
    Views:
    656
Loading...

Share This Page