Net/http - Confirming the right implementation

Hey folks, I am trying to use net/http and net/https to implement a
specific POST call for youtube’s video upload service (am trying to use
the direct upload API) and am looking to confirm that I am implementing
the net/http library properly to do this. The full POST call is as
follows (this comes from the youtube API):

POST /feeds/api/users/default/uploads HTTP/1.1
Authorization: ClientLogin auth=my_auth_token_here
X-GData-Key: key=my_dev_id_here
Slug: videofilename.mpg
Content-Type: multipart/related; boundary=“f93dcbA3”
Content-Length: 1941255
Connection: close

Content-Type: application/atom+xml; charset=UTF-8

<?xml version="1.0"?>

type=“plain”>My Video Title Here</media:title>
My Video Description Here

Content-Type: video/mp4
Content-Transfer-Encoding: binary


Here is the code I am using to implement this, am hoping to make sure
that I am using net/http correctly to pass all of the data where it
needs to be.

dev_id = ‘my_YT_dev_id_here’
upload_host = ‘
upload_path = ‘/feeds/api/users/my_username_here/uploads’

youtube_direct_upload( upload_host, upload_path, dev_id, video )

def youtube_direct_upload( host, path, dev_id, token, video )
data = <<“DATA”
Content-Type: application/atom+xml; charset=UTF-8

<?xml version="1.0"?>

<media:title type=“plain”>My Video Test</media:title>
<media:description type=“plain”>
Video Upload Test
media:keywordstest, video</media:keywords>

Content-Type: video/mpg
Content-Transfer-Encoding: binary


http = host, 443 )
http.use_ssl = true

headers = {
‘Host’ => “#{host}”,
‘Authorization’ => “GoogleLogin auth=#{token}”,
‘X-GData-Key’ => “key=#{dev_id}”,
‘Slug’ => ‘videofile.mpg’,
‘Content-Type’ => ‘multipart/related; boundary=“f93dcbA3”’,
‘Content-Length’ => data.length.to_s,
‘Connection’ => ‘close’

resp, data = path, data, headers )

puts 'Code: ’ + resp.code
puts 'Data: ’
puts data


I left out the code which rerieves the auth_token from the youtube
server as I have tested that aspect and it works fine, only having
trouble getting the above to work, when I try it I get this string of

/usr/local/lib/ruby/1.8/openssl/buffering.rb:178:in syswrite': Broken pipe (Errno::EPIPE) from /usr/local/lib/ruby/1.8/openssl/buffering.rb:178:indo_write’
from /usr/local/lib/ruby/1.8/openssl/buffering.rb:192:in write' from /usr/local/lib/ruby/1.8/net/protocol.rb:175:inwrite0’
from /usr/local/lib/ruby/1.8/net/protocol.rb:151:in write' from /usr/local/lib/ruby/1.8/net/protocol.rb:166:inwriting’
from /usr/local/lib/ruby/1.8/net/protocol.rb:150:in write' from /usr/local/lib/ruby/1.8/net/http.rb:1514:insend_request_with_body’
from /usr/local/lib/ruby/1.8/net/http.rb:1496:in exec' from /usr/local/lib/ruby/1.8/net/http.rb:1044:inrequest’
from /usr/local/lib/ruby/1.8/net/http.rb:1033:in request' from /usr/local/lib/ruby/1.8/net/http.rb:545:instart’
from /usr/local/lib/ruby/1.8/net/http.rb:1031:in request' from /usr/local/lib/ruby/1.8/net/http.rb:840:inpost’
from youtube_auth.rb:95:in `youtube_direct_upload’

I am hoping that I have simply implemented the net/http library
incorrectly, but am not sure… Any help would be greatly appreciated.


Hmm… Have you tried taking your post out of the
youtube_direct_upload method to see if you get the same error? Maybe
try these one by one to see if you can narrow it down:

Get rid of the headers
Get rid of the post body

Also, I assume that you have require ‘net/http’ and require ‘net/
https’ somewhere in there. You’d probably be getting a different
error if not.

The comments that I have around line 840 in http.rb say that the post
data must be a string. Maybe your video content is causing problems
(which would be a problem!).


I wanted to post a followup to this, I finally realized that I was
trying to use an ssl connection to the uploads. server for the direct
upload api when that was only needed for obtaining the ClientLogin auth
key. Replacing that with a normal http:// call solved the issue.


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