# Net/http - Confirming the right implementation

**URL:** <https://www.ruby-forum.com/t/net-http-confirming-the-right-implementation/134911>\
**Category:** Ruby\
**Created:** [April 16, 2008, 3:22am UTC](https://www.ruby-forum.com/t/net-http-confirming-the-right-implementation/134911 "2008-04-16T03:22:58Z")\
**Posts on this page:** 3\
**Page:** 1

<div class="post-metadata">

**Author:** ![aginghipster](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/a/e9c0ed/32.png) [@aginghipster](https://www.ruby-forum.com/u/aginghipster)\
**Post date:** [April 16, 2008, 3:22am UTC](https://www.ruby-forum.com/t/net-http-confirming-the-right-implementation/134911/1 "2008-04-16T03:22:58Z")

</div>

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  
Host: [uploads.gdata.youtube.com](http://uploads.gdata.youtube.com)  
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

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

\<?xml version="1.0"?\>

\<entry  
[http://code.google.com/apis/youtube/reference.html#youtube\_data\_api\_tag\_entry](http://code.google.com/apis/youtube/reference.html#youtube_data_api_tag_entry)  
xmlns=“[Atom Syndication Format namespace](http://www.w3.org/2005/Atom)”  
xmlns:media=“[http://search.yahoo.com/mrss/](http://search.yahoo.com/mrss/)”  
xmlns:yt=“[http://gdata.youtube.com/schemas/2007](http://gdata.youtube.com/schemas/2007)”\>  
\<media:group  
[http://code.google.com/apis/youtube/reference.html#youtube\_data\_api\_tag\_media:group](http://code.google.com/apis/youtube/reference.html#youtube_data_api_tag_media:group)\>  
\<media:title  
[http://code.google.com/apis/youtube/reference.html#youtube\_data\_api\_tag\_media:title](http://code.google.com/apis/youtube/reference.html#youtube_data_api_tag_media:title)  
type=“plain”\>My Video Title Here\</media:title\>  
\<media:description  
[http://code.google.com/apis/youtube/reference.html#youtube\_data\_api\_tag\_media:description](http://code.google.com/apis/youtube/reference.html#youtube_data_api_tag_media:description)  
type=“plain”\>  
My Video Description Here  
\</media:description\>  
\<media:category  
[http://code.google.com/apis/youtube/reference.html#youtube\_data\_api\_tag\_media:category](http://code.google.com/apis/youtube/reference.html#youtube_data_api_tag_media:category)  
scheme=“[http://gdata.youtube.com/schemas/2007/categories.cat](http://gdata.youtube.com/schemas/2007/categories.cat)”\>People  
\</media:category\>  
\<media:keywords  
[http://code.google.com/apis/youtube/reference.html#youtube\_data\_api\_tag\_media:keywords](http://code.google.com/apis/youtube/reference.html#youtube_data_api_tag_media:keywords)\>cat\_keyword1,  
cat\_keyword2\</media:keywords\>  
\</media:group\>  
  
–f93dcbA3  
Content-Type: video/mp4  
Content-Transfer-Encoding: binary

full\_video\_file\_binary\_contents\_here  
–f93dcbA3–

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 = ‘[uploads.gdata.youtube.com](http://uploads.gdata.youtube.com)’  
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”  
–f93dcbA3  
Content-Type: application/atom+xml; charset=UTF-8

\<?xml version="1.0"?\>

media:group  
\<media:title type=“plain”\>My Video Test\</media:title\>  
\<media:description type=“plain”\>  
Video Upload Test  
\</media:description\>  
\<media:category  
scheme=“[http://gdata.youtube.com/schemas/2007/categories.cat](http://gdata.youtube.com/schemas/2007/categories.cat)”\>People  
\</media:category\>  
media:keywordstest, video\</media:keywords\>  
\</media:group\>  
  
–f93dcbA3  
Content-Type: video/mpg  
Content-Transfer-Encoding: binary

#{video}  
–f93dcbA3–  
DATA

http = Net::HTTP.new( 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 = http.post( path, data, headers )

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

end

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  
errors:

/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:in `do\_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:in `write0’  
from /usr/local/lib/ruby/1.8/net/protocol.rb:151:in `write' from /usr/local/lib/ruby/1.8/net/protocol.rb:166:in `writing’  
from /usr/local/lib/ruby/1.8/net/protocol.rb:150:in `write' from /usr/local/lib/ruby/1.8/net/http.rb:1514:in `send\_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:in `request’  
from /usr/local/lib/ruby/1.8/net/http.rb:1033:in `request' from /usr/local/lib/ruby/1.8/net/http.rb:545:in `start’  
from /usr/local/lib/ruby/1.8/net/http.rb:1031:in `request' from /usr/local/lib/ruby/1.8/net/http.rb:840:in `post’  
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.

Thanks,  
Andy

---

<div class="post-metadata">

**Author:** ![aginghipster](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/a/e9c0ed/32.png) [@aginghipster](https://www.ruby-forum.com/u/aginghipster)\
**Post date:** [April 16, 2008, 6:29pm UTC](https://www.ruby-forum.com/t/net-http-confirming-the-right-implementation/134911/2 "2008-04-16T18:29:40Z")

</div>

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!).

-Kyle

---

<div class="post-metadata">

**Author:** ![aginghipster](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/a/e9c0ed/32.png) [@aginghipster](https://www.ruby-forum.com/u/aginghipster)\
**Post date:** [April 16, 2008, 6:35pm UTC](https://www.ruby-forum.com/t/net-http-confirming-the-right-implementation/134911/3 "2008-04-16T18:35:00Z")

</div>

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.

-Andy
