Encoding a multipart/form-data for posting via HTTP

Discussion in 'Ruby' started by Dema, Mar 10, 2005.

  1. Dema

    Dema Guest

    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, Mar 10, 2005
    #1
    1. Advertisements

  2. Dema

    Dave Burt Guest

    It sounds to me as if this should be in Net::HTTP::post.

    "Dema" <> 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
    >
     
    Dave Burt, Mar 10, 2005
    #2
    1. Advertisements

  3. Dema

    Dema Guest

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

    I agree completely.
     
    Dema, Mar 10, 2005
    #3
  4. Dema

    Guest

    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
     
    , Aug 22, 2012
    #4
    1. Advertisements

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. Jon Petrak

    ASP, multipart/form-data encoding

    Jon Petrak, Mar 1, 2004, in forum: ASP .Net
    Replies:
    3
    Views:
    9,107
    Jon Petrak
    Mar 5, 2004
  2. Axel Heck
    Replies:
    1
    Views:
    2,741
    Thomas Matthews
    Oct 20, 2003
  3. Jeff Shannon
    Replies:
    4
    Views:
    1,212
    Wade Leftwich
    Jul 21, 2004
  4. Nelson Minar

    multipart/form-data in an HTTP client

    Nelson Minar, Sep 1, 2004, in forum: Python
    Replies:
    5
    Views:
    6,141
    Jeff Shannon
    Sep 2, 2004
  5. Ames Andreas (MPA/DF)

    Re: multipart/form-data in an HTTP client

    Ames Andreas (MPA/DF), Sep 2, 2004, in forum: Python
    Replies:
    1
    Views:
    571
    Jeff Shannon
    Sep 2, 2004
  6. Clark C. Evans
    Replies:
    1
    Views:
    2,366
    Fuzzyman
    Jan 14, 2005
  7. Chris Morris
    Replies:
    2
    Views:
    237
    Chris Morris
    Sep 10, 2003
  8. Kevin DeValck
    Replies:
    1
    Views:
    1,063
    7stud --
    May 17, 2011
Loading...