posting multipart form through http.rb

Discussion in 'Ruby' started by Chris Morris, Sep 10, 2003.

  1. Chris Morris

    Chris Morris Guest

    In order to post multipart/form-data, I've got to build data like:

    Content-Type: multipart/form-data;
    boundary=---------------------------12199339629315
    Content-Length: 402

    -----------------------------12199339629315
    Content-Disposition: form-data; name="upfile"; filename="test.txt"
    Content-Type: text/plain

    [snip ... file data]

    -----------------------------12199339629315--

    I do this, then do the following with http.rb:

    # data equals the above stuff
    # @h is my http object
    req = Net::HTTP::post.new(get_url)
    resp = @h.request(req, data)


    In this case, http.rb seems to get in the way. Way down under the
    @h.request call we come to this:

    def send_request_with_body( sock, ver, path, body )
    @header['content-length'] = body.length.to_s
    @header.delete 'transfer-encoding'

    unless @header['content-type']
    $stderr.puts 'net/http: warning: Content-Type did not set; ...
    [snip]
    @header['content-type'] = 'application/x-www-form-urlencoded'
    end

    request sock, ver, path
    sock.write body
    end

    So, now I'm sending the following packets to the web server:

    POST /my.cgi.rb HTTP/1.1
    Connection: close
    Content-Type: application/x-www-form-urlencoded
    Authorization: Basic cnNxbDpkZyBkdWxsIGFncmVlIG5vb2sgaHVuaw==
    Content-Length: 514
    Host: iisportaldev01

    Content-Type: multipart/form-data;
    boundary=---------------------------12199339629315
    Content-Length: 402
    [snip]

    http.rb is unfortunately assuming I need Content-Type and Content-Length
    headers added, even though I'm taking care of them myself in my data.
    I'm not seeing a way to tell http.rb to stop with the "Content-" headers
    as it appears to be messing things up.

    --

    Chris
    http://clabs.org/blogki
    Chris Morris, Sep 10, 2003
    #1
    1. Advertising

  2. Chris Morris

    ts Guest

    >>>>> "C" == Chris Morris <> writes:

    C> # data equals the above stuff
    C> # @h is my http object
    C> req = Net::HTTP::post.new(get_url)

    Net::HTTP::post::new give you the possibility to add headers lines, try
    with

    headers = {}
    headers['Content-Type'] = ''multipart/form-data; etc... '
    req = Net::HTTP::post.new(get_url, headers)

    and remove these headers lines (Content-*) in the data that you send


    Guy Decoux
    ts, Sep 10, 2003
    #2
    1. Advertising

  3. Chris Morris

    Chris Morris Guest

    ts wrote:

    >C> # data equals the above stuff
    >C> # @h is my http object
    >C> req = Net::HTTP::post.new(get_url)
    >
    > Net::HTTP::post::new give you the possibility to add headers lines, try
    > with
    >
    > headers = {}
    > headers['Content-Type'] = ''multipart/form-data; etc... '
    > req = Net::HTTP::post.new(get_url, headers)
    >
    > and remove these headers lines (Content-*) in the data that you send
    >
    >

    Yeah, I just realized this ... I'd misassumed the Content-Type and
    -Length headers weren't just normal headers that could go anywhere in
    the http headers portion, based on one packet sniff I got from a
    browser. Sniffed another browser and saw a different arrangement, so,
    it's not working for me yet, but my original post was bunk.

    --

    Chris
    http://clabs.org/blogki
    Chris Morris, Sep 10, 2003
    #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. Li Zhang
    Replies:
    4
    Views:
    6,042
    softip
    Feb 27, 2009
  2. Axel Heck
    Replies:
    1
    Views:
    2,614
    Thomas Matthews
    Oct 20, 2003
  3. Jeff Shannon
    Replies:
    4
    Views:
    1,015
    Wade Leftwich
    Jul 21, 2004
  4. Dema
    Replies:
    3
    Views:
    322
  5. Replies:
    1
    Views:
    389
Loading...

Share This Page