http-access2 multi-part from upload

A

Ara.T.Howard

i'm trying to do something like

form = {
"foo" => "bar",
"bar" => "foo",
"file" => IO::read(file),
}

client.post_content uri, form

eg. mixed form input. what's the mechanism to get http-access2 to turn this
request into a multipart form upload?

regards.

-a
--
===============================================================================
| email :: ara [dot] t [dot] howard [at] noaa [dot] gov
| phone :: 303.497.6469
| anything that contradicts experience and logic should be abandoned.
| -- h.h. the 14th dalai lama
===============================================================================
 
D

Dan Fitzpatrick

Hey Ara,

It is very similar to multipart email encoding:

Add this to the post headers:

CONTENT_LENGTH: #{the total length or the body of the message with all
boundaries}
CONTENT_TYPE: multipart/form-data;
boundary=---------------------------41184676334

Then have a blank line. Then the body:

-----------------------------41184676334
Content-Disposition: form-data; name="file"; filename="example.txt"
Content-Type: text/plain

This is the content of a text file.

-----------------------------41184676334
Content-Disposition: form-data; name="foo"

bar
-----------------------------41184676334
Content-Disposition: form-data; name="bar"

foo
-----------------------------41184676334--


Don't forget the trailing -- after the last divider.

You could also use curl on the command line:

curl -F foo=bar -F bar=foo file=@/path/to/file uri

Dan
 
A

Ara.T.Howard

It is very similar to multipart email encoding:

Add this to the post headers:

CONTENT_LENGTH: #{the total length or the body of the message with all
boundaries}
CONTENT_TYPE: multipart/form-data;
boundary=---------------------------41184676334

Then have a blank line. Then the body:

-----------------------------41184676334
Content-Disposition: form-data; name="file"; filename="example.txt"
Content-Type: text/plain

This is the content of a text file.

-----------------------------41184676334
Content-Disposition: form-data; name="foo"

bar
-----------------------------41184676334
Content-Disposition: form-data; name="bar"

foo
-----------------------------41184676334--


Don't forget the trailing -- after the last divider.

thanks dan. i just couldn't figure out how to tell http-access2 that my form
should be encoded that way. turns out it's quite easy:

form = {
"username" => "ahoward",
"file_0" => open("foobar"),
"file_1" => open("barfoo),
}

boundary = Array::new(8){ "%2.2d" % rand(42) }.join('__')
extheader['content-type'] = "multipart/form-data; boundary=___#{ boundary }___"

response = client.send post_content, uri, form, extheader

and http-access2 does all the work for you... note that the values for file_0
and file_1 are io objects.... pretty neat!

You could also use curl on the command line:

curl -F foo=bar -F bar=foo file=@/path/to/file uri

i was actually porting my intiial attempt, which used curl, to pure ruby so
people without curl could use it. also, and this of off-topic but important,
i had three different versions of curl and all three required different
command line flags to get going, with only two versions being able to actually
upload to rubyforge due to a bug in cookied handling. for instance this
command (modified):

curl 'http://rubyforge.org/frs/admin/qrs.php' --silent --show-error
--user-agent 'Mozilla/4.0' --cookie-jar cookie_jar --cookie cookie_jar
--location-trusted --user 'ahoward:xxx' -F 'processor_id=8000' -F
'submit=Release File' -F 'preformatted=1' -F 'type_id=5000' -F 'group_id=1024'
-F '[email protected]' -F 'release_name=42.42.42' -F 'package_id=traits' -F
'release_date=2005-11-04 07:14'

works fine on 7.12.x but fails for 7.13.x and 7.15.x. oddly, using the 7.15.x
version running like this:

strace curl 'http://rubyforge.org/frs/admin/qrs.php' --silent --show-error
--user-agent 'Mozilla/4.0' --cookie-jar cookie_jar --cookie cookie_jar
--location-trusted --user 'ahoward:xxx' -F 'processor_id=8000' -F
'submit=Release File' -F 'preformatted=1' -F 'type_id=5000' -F
'group_id=1024' -F '[email protected]' -F 'release_name=42.42.42'
-F 'package_id=traits' -F 'release_date=2005-11-04 07:14'

in the first case the cookies are not sent to the client and the cookie file
is not written. in the second case it is. note that these are precisely the
same command - only one is run under strace.

has anyone else ever seen somthing like this?

in any case i decided, like i generally do, that prorgrams that are doing
massive amounts of string manipulation and io in c are simply evil and cannot
be trusted or fixed easily. as it were i found, and fixed, a bug in
http-access2 and got it working in about an hour. ;-)

cheers.

-a
--
===============================================================================
| email :: ara [dot] t [dot] howard [at] noaa [dot] gov
| phone :: 303.497.6469
| anything that contradicts experience and logic should be abandoned.
| -- h.h. the 14th dalai lama
===============================================================================
 

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,756
Messages
2,569,534
Members
45,007
Latest member
OrderFitnessKetoCapsules

Latest Threads

Top