Looking for a net/http example

A

Andrew Cowan

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
 
A

Andrew Cowan

Thanks for posting that!

-Andy


Park said:
Hi,
----- Original Message -----
From: "Andrew Cowan" <[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 Heesob
 

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

Forum statistics

Threads
474,290
Messages
2,571,453
Members
48,131
Latest member
AntoniaSep

Latest Threads

Top