java - Uploading a file to Box API using curl -
i try upload file box, using box api. whatever try, receive 400 bad request
without other information. idea problem?
curl https://upload.box.com/api/2.0/files/content \ -h "authorization: bearer access_token" -x post \ -f attributes='{"name":"tigers.jpeg", "parent":{"id":"11446498"}}' \ -f file=@myfile.jpg
try { string url = "https://upload.box.com/api/2.0/files/content"; webresource resource = client.resource(url); jsonobject parentinput = new jsonobject(); parentinput.put("id", "0"); jsonobject inputjson = new jsonobject(); filename = filename.trim(); inputjson.put("name", filename); inputjson.put("parent", parentinput); input = inputjson.tostring(); formdatamultipart form = new formdatamultipart(); string cdmeta = "form-data; name=\"attributes\";"; string cdfile = "form-data; name=\""+filename+"\";"; formdatacontentdisposition cdmetaobj = new formdatacontentdisposition(cdmeta); formdatabodypart metaddbpart = new formdatabodypart(cdmetaobj, input); form.bodypart(metaddbpart); formdatacontentdisposition cdfileobj = new formdatacontentdisposition(cdfile); formdatabodypart fileddbpart = new formdatabodypart("file", inputstream, mediatype.application_octet_stream_type); fileddbpart.setcontentdisposition(cdfileobj); form.bodypart(fileddbpart); //here using builder executing client request builder builder = resource.header("content-type", mediatype.multipart_form_data).header("authorization","bearer " + accesstoken); clientresponse response = builder.post(clientresponse.class, form); string result = response.getentity(string.class); } catch (exception e) { e.printstacktrace(e); }
i got solution, it's upload small change. problem here manage contentdisposition properly
try { string url = "https://upload.box.com/api/2.0/files/content"; webresource resource = client.resource(url); jsonobject parentinput = new jsonobject(); parentinput.put("id", "0"); jsonobject inputjson = new jsonobject(); filename = filename.trim(); inputjson.put("name", filename); inputjson.put("parent", parentinput); input = inputjson.tostring(); formdatamultipart form = new formdatamultipart(); string cdmeta = "form-data; name=\"metadata\";"; string cdfile = "form-data; name=\"content\"; filename = \"" + filename + "\";"; formdatacontentdisposition cdmetaobj = new formdatacontentdisposition(cdmeta); formdatabodypart metaddbpart = new formdatabodypart(cdmetaobj, input); form.bodypart(metaddbpart); formdatacontentdisposition cdfileobj = new formdatacontentdisposition(cdfile); formdatabodypart fileddbpart = new formdatabodypart("file", inputstream, mediatype.application_octet_stream_type); fileddbpart.setcontentdisposition(cdfileobj); form.bodypart(fileddbpart); //here using builder executing client request builder builder = resource.header("content-type", mediatype.multipart_form_data).header("authorization", "bearer " + accesstoken); clientresponse response = builder.post(clientresponse.class, form); string result = response.getentity(string.class); } catch (exception e) { e.printstacktrace(e); } }
Comments
Post a Comment