Looking for a net/http example

Hey folks, I am hoping someone can post a quick net/http example which
illustrates a POST method call with multipart/related content, I think I
am confused as to which info is specified in the headers and which is
passed as data, an example would really hep and I’d be very grateful.

Thanks,
Andy

Hi,
----- Original Message -----
From: “Andrew C.” [email protected]
To: “ruby-talk ML” [email protected]
Sent: Thursday, April 17, 2008 12:15 AM
Subject: Looking for a net/http example

Hey folks, I am hoping someone can post a quick net/http example which
illustrates a POST method call with multipart/related content, I think I
am confused as to which info is specified in the headers and which is
passed as data, an example would really hep and I’d be very grateful.

Thanks,
Andy

Here is some code snippet from gmailer.rb

       postdata = {}
       postdata["msgbody"] = stripslashes(body)
       postdata["from"] = stripslashes(from) if from
       postdata["to"] = stripslashes(to)
       postdata["subject"] = stripslashes(subject)
       postdata["cc"] = stripslashes(cc)
       postdata["bcc"] = stripslashes(bcc)

       boundary = 

“----#{Time.now.to_i}#{(rand(2000)*2147483.648).to_i}”
postdata2 = []
postdata.each {|k,v|
postdata2.push(“Content-Disposition: form-data;
name=”#{k}"\r\n\r\n#{v}\r\n")
}

       files.each_with_index do |f,i|
          content = File.open(f,'rb') { |c| c.read }
          postdata2.push("Content-Disposition: form-data; 

name=“file#{i}”; filename="#{File.basename(f)}"\r\n" +
“Content-Transfer-Encoding: binary\r\n” +
“Content-Type: application/octet-stream\r\n\r\n” +
content + “\r\n”)
end

       postdata = postdata2.collect { |p|
             "--" + boundary + "\r\n" + p
       }.join('') + "--" + boundary + "--\r\n"

       np = 

Net::HTTP::Proxy(@proxy_host,@proxy_port,@proxy_user,@proxy_pass).new(GM_LNK_HOST,
80)
response = nil
np.start { |http|
response = http.post(GM_LNK_GMAIL, postdata,{‘Cookie’ =>
@cookie_str,‘User-agent’ => GM_USER_AGENT,‘Content-type’ =>
‘multipart/form-data; boundary=’ + boundary } )
}

If you want full source, try gem install gmailer

Regards,
Park H.

Thanks for posting that!

-Andy

This forum is not affiliated to the Ruby language, Ruby on Rails framework, nor any Ruby applications discussed here.

| Privacy Policy | Terms of Service | Remote Ruby Jobs