# Youtube File upload issue

**URL:** <https://www.ruby-forum.com/t/youtube-file-upload-issue/195467>\
**Category:** Ruby\
**Created:** [September 23, 2010, 3:05pm UTC](https://www.ruby-forum.com/t/youtube-file-upload-issue/195467 "2010-09-23T15:05:08Z")\
**Posts on this page:** 2\
**Page:** 1

<div class="post-metadata">

**Author:** ![itkin](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/i/22d042/32.png) [@itkin](https://www.ruby-forum.com/u/itkin)\
**Post date:** [September 23, 2010, 3:05pm UTC](https://www.ruby-forum.com/t/youtube-file-upload-issue/195467/1 "2010-09-23T15:05:08Z")

</div>

Hello,

I’m triying to upload video to google api via direct upload  
([http://code.google.com/intl/fr/apis/youtube/2.0/developers\_guide\_protocol.html#Direct\_uploading](http://code.google.com/intl/fr/apis/youtube/2.0/developers_guide_protocol.html#Direct_uploading)).

I’m using an rails active resource class and would lie to handle the  
video uploading in a custom method with Nett::HTTP

Here is google requirement :

POST /feeds/api/users/default/uploads HTTP/1.1  
Host: [uploads.gdata.youtube.com](http://uploads.gdata.youtube.com)  
Authorization: AuthSub token=“DXAA…sdb8”  
GData-Version: 2  
X-GData-Key: key=adf15ee97731bca89da876c…a8dc  
Slug: video-test.mp4  
Content-Type: multipart/related; boundary=“f93dcbA3”  
Content-Length: 1941255  
Connection: close

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

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

media:group  
\<media:title type=“plain”\>Bad Wedding Toast\</media:title\>  
\<media:description type=“plain”\>  
I gave a bad toast at my friend’s wedding.  
\</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:keywordstoast, wedding\</media:keywords\>  
\</media:group\>  
  
–f93dcbA3  
Content-Type: video/mp4  
Content-Transfer-Encoding: binary

--f93dcbA3--

Here is my request object before to be sent :  
(irb)\> y request

body: |  
–RubyMultipartClient901619ZZZZZ  
Content-Type: application/atom+xml; charset=UTF-8

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

media:group  
\<media:title type=“plain”\>test 7\</media:title\>  
\<media:description type=“plain”\>\</media:description\>  
\<media:category  
scheme=“[http://gdata.youtube.com/schemas/2007/categories.cat](http://gdata.youtube.com/schemas/2007/categories.cat)”\>Film\</media:category\>  
media:keywords\</media:keywords\>  
\</media:group\>  
  
–RubyMultipartClient901619ZZZZZ  
Content-Type: video/mp4  
Content-Transfer-Encoding: binary

–RubyMultipartClient901619ZZZZZ–

body\_stream:  
header:  
slug:

- 01.mp4  
x-gdata-key:
- 

key=AI39si57I1WXXFZSHlDFjrfUgBapS7DoJ0hPppuqgC2FY\_vK-nUQ8Iw7lTUJTdqwSCstyIhSWBk-C9JI1DCQolrmVvTcj6xvLg  
content-type:

- multipart/related; boundary=“RubyMultipartClient901619ZZZZZ”  
authorization:
- AuthSub token=“1/qqpJVDJ4Gziq6pLv8tmMWgIv4zxvwk3sZP9PiVHlCt4”  
gdata-version:
- “2”  
content-length:
- “672”  
method: POST  
path: /feeds/api/users/default/uploads  
request\_has\_body: true  
response\_has\_body: true

I triyed to read the file and include its content in the body post, also  
tryed to include it after encoding in base64  
(Base64.encode64(file.read))

The request is build and sent as follow :

uri =  
URI.parse(“[http://uploads.gdata.youtube.com/feeds/api/users/default/uploads](http://uploads.gdata.youtube.com/feeds/api/users/default/uploads)”)  
http = Net::HTTP.new(uri.host, uri.port)  
request = Net::HTTP::Post.new(uri.request\_uri)  
request.initialize\_http\_header(headers)  
request.body = post\_body  
response = http.request(request)

But it keeps responding :

#\<Net::HTTPBadRequest 400 Bad Request readbody=true\>

I don’t know what’s wrong i’m completely stuck, could you give me a hand  
?

---

<div class="post-metadata">

**Author:** ![itkin](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/i/22d042/32.png) [@itkin](https://www.ruby-forum.com/u/itkin)\
**Post date:** [September 23, 2010, 11:54pm UTC](https://www.ruby-forum.com/t/youtube-file-upload-issue/195467/2 "2010-09-23T23:54:53Z")

</div>

I succeeded to make it work, by setting the post body as follow :

data = %{–bbe873dc\r  
Content-Type: application/atom+xml; charset=UTF-8

#{xml\_entry}\r  
–bbe873dc\r  
Content-Type: #{meta[:file].content\_type}  
Content-Transfer-Encoding: binary

#{meta[:file].read}\r  
–bbe873dc–\r\n}

I still don’t understand why it wasn’t working properly before … 😕
