Velocity Reviews

Velocity Reviews (http://www.velocityreviews.com/forums/index.php)
-   Ruby (http://www.velocityreviews.com/forums/f66-ruby.html)
-   -   Encoding a multipart/form-data for posting via HTTP (http://www.velocityreviews.com/forums/t820226-encoding-a-multipart-form-data-for-posting-via-http.html)

Dema 03-10-2005 07:14 AM

Encoding a multipart/form-data for posting via HTTP
 
After googling around (with no luck) for a while for a function that
would encode a hash into a multipart/form-data to be sent to a HTTP
server via POST, I decided to read the RFC and write my own. In fact,
it's not that complicated and I would like to share it just in case
someone else needs it:

BOUNDARY = "AaB03x"
def encode_multipartformdata(parameters = {})
ret = String.new
parameters.each do |key, value|
unless value.empty?
ret << "\r\n--" << BOUNDARY << "\r\n"
ret << "Content-Disposition: form-data; name=\"#{key}\"\r\n\r\n"
ret << value
end
end
ret << "\r\n--" << BOUNDARY << "--\r\n"
end

I got stuck for a while on the boundary syntax and the \r\n stuff, but
eventually it worked ok.

I just needed it to send some data to a RDF database that offers a HTTP
API channel. So, this only works for the simplest case. There are some
more complex variations, like uploading one or more files, setting
individual content types for each part, etc.

rgds
Dema


Dave Burt 03-10-2005 08:47 AM

Re: Encoding a multipart/form-data for posting via HTTP
 
It sounds to me as if this should be in Net::HTTP::Post.

"Dema" <demetriusnunes@gmail.com> offered:
> After googling around (with no luck) for a while for a function that
> would encode a hash into a multipart/form-data to be sent to a HTTP
> server via POST, I decided to read the RFC and write my own. In fact,
> it's not that complicated and I would like to share it just in case
> someone else needs it:
>
> BOUNDARY = "AaB03x"
> def encode_multipartformdata(parameters = {})
> ret = String.new
> parameters.each do |key, value|
> unless value.empty?
> ret << "\r\n--" << BOUNDARY << "\r\n"
> ret << "Content-Disposition: form-data; name=\"#{key}\"\r\n\r\n"
> ret << value
> end
> end
> ret << "\r\n--" << BOUNDARY << "--\r\n"
> end
>
> I got stuck for a while on the boundary syntax and the \r\n stuff, but
> eventually it worked ok.
>
> I just needed it to send some data to a RDF database that offers a HTTP
> API channel. So, this only works for the simplest case. There are some
> more complex variations, like uploading one or more files, setting
> individual content types for each part, etc.
>
> rgds
> Dema
>




Dema 03-10-2005 06:23 PM

Re: Encoding a multipart/form-data for posting via HTTP
 

Dave Burt wrote:
> It sounds to me as if this should be in Net::HTTP::Post.
>

I agree completely.


pavankanth.m@gmail.com 08-22-2012 09:26 PM

Re: Encoding a multipart/form-data for posting via HTTP
 
Hi Dema -

Can you post the snippet of code in which you have used the above encoding logic?

I am writing a web application that needs to take a file upload from the user and upload it to a remote server. I can take input from user to server fine via file_field, but can't seem to work out the next step of uploading from server to remote

I am able to use HTTP:GET and download files from remote server and write to local disk, but not able to accomplish the opposite (local disk to remote server)


Thanks in advance.
Pavan


All times are GMT. The time now is 05:54 PM.

Powered by vBulletin®. Copyright ©2000 - 2014, vBulletin Solutions, Inc.
SEO by vBSEO ©2010, Crawlability, Inc.