posting multipart form through http.rb

C

Chris Morris

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.
 
T

ts

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
 
C

Chris Morris

ts said:
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.
 

Ask a Question

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

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,995
Messages
2,570,236
Members
46,822
Latest member
israfaceZa

Latest Threads

Top