Reputation: 2029
I'm trying to insert a file in google drive using webRequest (since I'm implementing an resumable async upload), but I have no idea how to put data in request "body".
From now, I have:
public static HttpWebRequest CreateUploadRequest(Google.Apis.Drive.v2.DriveService driveService, string uri, Stream contentStream, string title, string mimeType, string description = null)
{
HttpWebRequest request = (HttpWebRequest)WebRequest.Create(uri);
request.Method = "PUT";
Dictionary<string, object> requestBody = new Dictionary<string, object>();
requestBody["title"] = title;
requestBody["mimeType"] = mimeType;
if (!string.IsNullOrWhiteSpace(description))
{
requestBody["description"] = description;
}
driveService.Authenticator.ApplyAuthenticationToRequest(request);
Stream requestStream = request.GetRequestStream();
//How to do that???
requestStream.Close();
return request;
}
I set the headers for the HttpWebRequest, how the data of the body should be disposed? And whats is the property name for the byte[] data of the file to be inserted?
Any example whould be appreciated.
Upvotes: 1
Views: 1338
Reputation: 598775
The SDK documentation on this page says:
The body of the request is formatted as a multipart/related content type RFC2387 and contains exactly two parts. The parts are identified by a boundary string, and the final boundary string is followed by two hyphens.
If you've never worked with it, RFC2387 is the MIME definition. Binary data in MIME is typically BASE64 encoded, like in this example from the RFC:
Content-Type: Application/octet-stream
Content-Description: The fixed length records
Content-Transfer-Encoding: base64
Content-ID: <[email protected]>
T2xkIE1hY0RvbmFsZCBoYWQgYSBmYXJtCkUgSS
BFIEkgTwpBbmQgb24gaGlzIGZhcm0gaGUgaGFk
IHNvbWUgZHVja3MKRSBJIEUgSSBPCldpdGggYS
BxdWFjayBxdWFjayBoZXJlLAphIHF1YWNrIHF1
YWNrIHRoZXJlLApldmVyeSB3aGVyZSBhIHF1YW
NrIHF1YWNrCkUgSSBFIEkgTwo=
Have a look at this question for an example of uploading binary data to a Web request: Upload files with HTTPWebrequest (multipart/form-data)
Upvotes: 3