i'm trying implement rest client google drive jersey 2.0.
according following document, made code sends files 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 post request 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 sent following error occurred.
{ "error": { "errors": [ { "domain": "global", "reason": "parseerror", "message": "parse error" } ], "code": 400, "message": "parse error" } }
please tell me how avoid error.
an upload request should have 2 multi-parts: metadata , file content , metadata part should in json.
in case create new form-encoded part each attribute. use reference on docs see 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 valid json.
Comments
Post a Comment