Posting multipart form through http.rb

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

      # data equals the above stuff
      # @h is my http object
      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 wrote:

  # data equals the above stuff
  # @h is my http object
  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