Reputation: 33
I try to upload a file to Box, using Box API.
But whatever I try, I always receive 400 Bad Request without any other information.
Any idea about the problem?
The example from the API is this curl request :
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 [email protected]
My code is below :
String URL = "https://upload.box.com/api/2.0/files/content/";
HttpClient httpClient = new HttpClient();
PostMethod postMethod = new PostMethod(URL);
postMethod.setRequestHeader("Authorization", "Bearer "+ this.token);
try {
List<Part> parts = new ArrayList<Part>();
JSONObject parent = new JSONObject();
parent.put("id", this.parentId);
JSONObject attributes = new JSONObject();
attributes.put("parent", parent);
attributes.put("name", file.getName());
StringPart strPart = new StringPart("attributes", attributes.toString());
strPart.setContentType("application/json");
parts.add(strPart);
ByteArrayPartSource source = new ByteArrayPartSource(file.getName(),
IOUtils.toByteArray(this.file);
parts.add(new FilePart("file", source));
postMethod.setRequestEntity(new MultipartRequestEntity(parts.toArray(new Part[0]), postMethod.getParams()));
httpClient.executeMethod(postMethod);
int status = postMethod.getStatusCode();
if (status == HttpURLConnection.HTTP_OK || status == HttpURLConnection.HTTP_ACCEPTED) {
String jsonText = postMethod.getResponseBodyAsString();
JSONObject json = new JSONObject(jsonText);
System.out.println(jsonText);
} else {
throw new MyException(postMethod.getResponseBodyAsString());
}
} catch (Exception e) {
e.printStackTrace();
} finally {
postMethod.releaseConnection();
}
Upvotes: 1
Views: 4018
Reputation: 335
BoxConfig boxConfig = BoxConfig.readFrom(new FileReader("box_config.json")); //.json configuration file can be downloaded from dev console based on your app settings
BoxAPIConnection api = BoxDeveloperEditionAPIConnection.
getAppUserConnection(USER_ID, boxConfig);
BoxFolder boxFolder = new BoxFolder(api, FOLDER_ID);
boxFolder.uploadFile(stream, filename);
Be aware that it is not enough to just create the App and set it to read and write, you also needs to authorize the app in admin console - Enterprise Settings. Use client Id to authorize the new app
Upvotes: 0
Reputation: 33
I found out the solution, the different parts were not correct. I had to create 3 parts:
This code works :
String URL = "https://upload.box.com/api/2.0/files/content";
HttpClient httpClient = new HttpClient();
PostMethod postMethod = new PostMethod(URL);
postMethod.setRequestHeader("Authorization", "Bearer "+ this.token);
try {
List<Part> parts = new ArrayList<Part>();
parts.add(new StringPart("parent_id", parentId));
JSONObject parent = new JSONObject();
parent.put("id", this.parentId);
JSONObject attributes = new JSONObject();
attributes.put("parent", parent);
attributes.put("name", file.getName());
StringPart strPart = new StringPart("metadata", attributes.toString());
strPart.setContentType("text/plain");
parts.add(strPart);
ByteArrayPartSource source = new ByteArrayPartSource(file.getName(),
IOUtils.toByteArray(this.file));
parts.add(new FilePart("file", source));
postMethod.setRequestEntity(new MultipartRequestEntity(parts.toArray(new Part[0]), postMethod.getParams()));
httpClient.executeMethod(postMethod);
// checks server's status code first
int status = postMethod.getStatusCode();
System.out.println(status);
if (status == HttpURLConnection.HTTP_OK || status == HttpURLConnection.HTTP_CREATED) {
String jsonText = postMethod.getResponseBodyAsString();
JSONObject json = new JSONObject(jsonText);
System.out.println(jsonText);
} else {
throw new MyException(postMethod.getResponseBodyAsString());
}
} catch (Exception e) {
e.printStackTrace();
} finally {
postMethod.releaseConnection();
}
Upvotes: 1
Reputation: 43
Have you validated the parent ID = 11446498 is valid? If you're just testing this endpoint, try with the ID = 0 which will represent the root folder.
Upvotes: 0