1
votes

I'm trying to implement REST client for Google Drive with Jersey 2.0.

According to following document, I made the code that sends files by multipart request.

https://developers.google.com/drive/v2/reference/files/insert https://developers.google.com/drive/manage-uploads

    String targetUrl = "https://www.googleapis.com/upload/drive/v2/files?uploadType=multipart&access_token=" + token.access_token;
    WebTarget target = client.target(targetUrl);
    final FileDataBodyPart filePart = new FileDataBodyPart("file", file);
    final MultiPart multipart = new FormDataMultiPart().field("title", file.getName())
        .field("description", file.getName())
        .bodyPart(filePart);
    Response response = target.request(MediaType.APPLICATION_JSON_TYPE)
            .post(Entity.entity(multipart, multipart.getMediaType()));
    System.out.println("response:" + response.readEntity(String.class));

Here's the POST request by the code.

POST /upload/drive/v2/files?uploadType=multipart&access_token={ACCESS_TOKEN} HTTP/1.1\r\n
Accept: application/json\r\n
Content-Type: multipart/form-data; boundary=Boundary_1_1833261898_1373877178038\r\n
User-Agent: Jersey/2.0 (HttpUrlConnection 1.7.0_25)\r\n
MIME-Version: 1.0\r\n
Host: www.googleapis.com\r\n
Content-Length: 42430\r\n

MIME Multipart Media Encapsulation, Type: multipart/form-data, Boundary: "Boundary_1_1833261898_1373877178038"
Type: multipart/form-data
First boundary: --Boundary_1_1833261898_1373877178038\r\n
Encapsulated multipart part:  (text/plain)
    Content-Type: text/plain\r\n
    Content-Disposition: form-data; name="title"\r\n\r\n
    Line-based text data: text/plain
Boundary: \r\n--Boundary_1_1833261898_1373877178038\r\n
Encapsulated multipart part:  (text/plain)
    Content-Type: text/plain\r\n
    Content-Disposition: form-data; name="description"\r\n\r\n
    Line-based text data: text/plain
Boundary: \r\n--Boundary_1_1833261898_1373877178038\r\n
Encapsulated multipart part:  (text/plain)
    Content-Type: text/plain\r\n
    Content-Disposition: form-data; filename="GoogleDrive.txt"; modification-date="Fri, 12 Jul 2013 08:15:47 GMT"; size=41918; name="file"\r\n\r\n
    Line-based text data: text/plain
Last boundary: \r\n--Boundary_1_1833261898_1373877178038--\r\n

My post request was sent but following error occurred.

{
 "error": {
  "errors": [
   {
    "domain": "global",
    "reason": "parseError",
    "message": "Parse Error"
   }
  ],
  "code": 400,
  "message": "Parse Error"
 }
}

Please tell me how to avoid this error.

1

1 Answers

0
votes

An upload request should have two multi-parts: metadata and file content and metadata part should be in JSON.

In your case you create a new form-encoded part for each attribute. Use the reference on docs to see the proper formatting: https://developers.google.com/drive/manage-uploads#multipart

final MultiPart multipart = new FormDataMultiPart().field("title", file.getName())
    .field("description", file.getName())
    .bodyPart(filePart);

Your multipart body should be valid JSON.