I finally decided to implement the videos-upload api into my library after a while of putting it off. Steps one and two went smoothly, however I’m hung up on step 3, uploading each part.
I have separated the video file into each 20MB segments and I’m passing the byte arrays to the following method:
public async Task<HttpStatusCode> UploadVideoPartAsync(string video_id, string part, string upload_token, byte[] data)
{
RestRequest request = Request("upload/{video_id}", Method.PUT, ApiVersion.v4);
request.AddHeader("Content-Length", data.Length.ToString());
request.AddUrlSegment("video_id", video_id);
request.AddQueryParameter("part", part);
request.AddQueryParameter("upload_token", upload_token);
request.RequestFormat = DataFormat.Json;
request.AddBody(data);
IRestResponse<object> response = await client.ExecuteTaskAsync<object>(request);
return response.StatusCode;
}
However, I always get status 413: Request entity too large. I double checked my byte arrays and they are definitely 20MB in size and am passing through the right byte array to the method. I’ve also tried just about every option to add a body with RestSharp, the rest request library that I’ve been using, yet nothing works and I always get status 413.
UPDATE
So it turns out with the newest version of RestSharp, you can specify custom Content-Types
through parameters as well as pass through the data to be sent without needing to use request.AddBody()
. This allows you to bypass the two native serialize method RestSharp uses, Json and XML, and send the raw bytes instead. By doing this I was able to upload the video in 20MB segments instead of 5MB using this very small modification to the method:
public async Task<HttpStatusCode> UploadVideoPartAsync(string video_id, string part, string upload_token, byte[] raw_data)
{
RestRequest request = Request("upload/{video_id}", Method.PUT, ApiVersion.v4);
request.AddUrlSegment("video_id", video_id);
request.AddQueryParameter("part", part);
request.AddQueryParameter("upload_token", upload_token);
//simple, yet effective
request.AddHeader("Content-Length", raw_data.Length.ToString());
request.AddParameter("application/bson", raw_data, ParameterType.RequestBody);
IRestResponse<object> response = await uploads_api_client.ExecuteTaskAsync<object>(request);
return response.StatusCode;
}