title: Uploading file using Google Drive API returns "400: Bad Request"
body: <p>I am trying to upload a file to Google Drive using the Google Drive API and Java.</p>
  
  <p>The file I'm trying to upload is a docx file (previously exported using the Drive API). My problem is that when uploading files that has filenames containing swedish characters (like åäö), the Drive API throws an exception.</p>
  
  <p>The code for uploading looks essentially like this:</p>
  
  <pre><code>File file = new File("/path/to/file.docx");
  String contentType = "application/vnd.openxmlformats-officedocument.wordprocessingml.document";
  
  File fileToUpload = new File()
              .setTitle("abc")
              .setMimeType(contentType);
  
  FileContent mediaContent = new FileContent(contentType, file);
  
  try {
      Drive.Files.Insert request = client.files().insert(fileToUpload, mediaContent);
      request.execute();
  } catch (IOException e) {
      logger.error("Could not restore file");
      throw e;
  }
  </code></pre>
  
  <p>This code actually works fine. <em>But</em> if I change setTitle("abc") to setTitle("abcö") I get this exception:</p>
  
  <pre><code>com.google.api.client.googleapis.json.GoogleJsonResponseException: 400 Bad Request
  Bad Request
  </code></pre>
  
  <p>I have tried using versions v2-rev82-1.14.2-beta and v2-rev82-1.15.0-rc of google-api-services-drive, with the same result.</p>
  
  <p>I am using OSX if that helps (Windows seems to have these kinds of problems more often).</p>
  
  <hr>
  
  <p>EDIT:
  After some experimenting I found that if I exclude the FileContent object (the actual file content), and only create an empty file uploading the meta data, then the swedish characters in the title are fine.</p>
  
  <pre><code>Drive.Files.Insert request = client.files().insert(fileToUpload);
  </code></pre>
  
  <p>Seems like it's not the title that's the main problem after all. If I just can figure out how to add the file data to the empty file, I should be done.</p>
  
  <hr>
  
  <h2>EDIT2: Solution found!</h2>
  
  <p>Adding: <code>request.getMediaHttpUploader().setDirectUploadEnabled(true);</code> enables direct upload (not sure what I was using before though), and apparently that makes Google Drive not care about my strange Swedish characters anymore (Again, not sure why).</p>
  
  <p>This is the code I ended up with (with simplifications):</p>
  
  <pre><code>File file = new File("/path/to/file.docx");
  String contentType = "application/vnd.openxmlformats-officedocument.wordprocessingml.document";
  
  File fileToUpload = new File()
          .setTitle("abcö")
          .setMimeType(contentType);
  
  FileContent mediaContent = new FileContent(contentType, file);
  
  try {
      Drive.Files.Insert request = client.files().insert(fileToUpload, mediaContent);
      request.getMediaHttpUploader().setDirectUploadEnabled(true);
      request.execute();
  } catch (IOException e) {
      logger.error("Could not restore file");
      throw e;
  }
  </code></pre>
  
  <p>Now I only need to add support for resumable uploads for big files, but that's a different story.</p>
  
  <p>EDIT3: By the way, <a href="https://code.google.com/p/google-api-java-client/wiki/MediaUpload" rel="nofollow">here are the docs</a> that got me on the right track.</p>