I’m getting error when I try to upload a file to google drive api with my web application and the file has an character with accent, like for example ‘ç’.
I’m uploading the file with two requests, an initial request that has the file info and return the uri to where the file should be uploaded, and the subsequent request with the file itself.
The error occurs in the first request (the one with file information).
The error returned is just an ‘400 – bad request’, and I have no idead how to upload the file with this kind of characteres.
Here is an example of request generated by my application when I try to upload an gif image file.
Content-Type: application/json; charset=UTF-8 X-Upload-Content-Length: 257 Authorization: Bearer ****** X-Upload-Content-Type: image/gif Host: www.googleapis.com Content-Length: 126 Expect: 100-continue
The request body contains only an json object with file data, heres the code example where this initial request is configured:
private WebRequest CreateInitialUploadRequest(String parentId, String title, String mimeType)
{
String uri = "https://www.googleapis.com/upload/drive/v2/files?uploadType=resumable&access_token=" + AUTH_KEY;
System.Net.WebRequest request = this.WebRequestFactory.CreateWebRequest(uri);
request.Method = "POST";
request.ContentType = "application/json; charset=UTF-8";
request.Headers.Add("X-Upload-Content-Length", length.ToString());
request.Headers.Add("Authorization", string.Format("Bearer {0}", AUTH_KEY));
request.Headers.Add("X-Upload-Content-Type", mimeType);
//Setup parent id folder for file
object[] parents = {new {id = parentId}};
object jsonObject = new
{
title = title,
mimeType = mimeType,
description = "foo bar",
parents = parents
};
String strData = new JavaScriptSerializer().Serialize(jsonObject);
byte[] data = Encoding.UTF8.GetBytes(strData);
request.ContentLength = data.Length;
var stream = request.GetRequestStream();
stream.Write(data, 0, data.Length);
stream.Close();
return request;
}
When I try to call the method like this:
WebRequest initialRequest = CreateInitialUploadRequest("valid-parent-folder", "invalid file ç", "image/gif");
//I get an 'bad request' http error code 400 in the following line:
initialRequest.GetResponse();
Should I encode the request somehow?
Answers:
Thank you for visiting the Q&A section on Magenaut. Please note that all the answers may not help you solve the issue immediately. So please treat them as advisements. If you found the post helpful (or not), leave a comment & I’ll get back to you as soon as possible.
Method 1
Following the peleyal’s commentary I solved my problem. I’m still using rest calls and not drive api, but I checked out the drive api source code and find out that they are using the HttpRequestMessage class. Changing my code from WebRequest to HttpRequestMessage solved the problem:
var request = new HttpRequestMessage(HttpMethod.Post, uri);
string strData = new JavaScriptSerializer().Serialize(jsonObject);
var content = new StringContent(strData, Encoding.UTF8, "application/json");
content.Headers.ContentType = new System.Net.Http.Headers.MediaTypeHeaderValue("application/json");
request.Content = content;
Method 2
Why don’t you use the Google APIs .NET client library? As you can see in our Media page we support resumable media upload which is very easy to use, and will handle server errors for you.
All methods was sourced from stackoverflow.com or stackexchange.com, is licensed under cc by-sa 2.5, cc by-sa 3.0 and cc by-sa 4.0