Ich versuche eine Datei mit Box API in Box hochzuladen.BOX API Hochladen einer Datei Java
Aber was immer ich versuche, ich bekomme immer 400 Bad Request ohne weitere Informationen.
Irgendeine Idee über das Problem?
Das Beispiel von der API ist dies curl Anfrage:
curl https://upload.box.com/api/2.0/files/content \
-H "Authorization: Bearer access_token" -X POST \
-F attributes = '{ "name":“ tigers.jpeg“, "Eltern": { "id": "11446498"}}‘ \
-F [email protected]
Mein Code ist unten:
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();
}
Die übergeordnete ID "11446498" ist das in der API verwendete Beispiel. Der, den ich in meinem Code verwende, ist gültig, ich habe es überprüft. – Neptune
Versuchen Sie, das abschließende "/" vom URL-Endpunkt zu entfernen. Der korrekte Endpunkt ist [https://upload.box.com/api/2.0/files/content](https://docs.box.com/reference#upload-a-file) für die [Dokumentation] (https: //docs.box.com/reference#upload-a-file). Die erste Zeile des Codes sollte sein: 'String URL = "https://upload.box.com/api/2.0/files/content";' – Brent
Sie Brent Dank, es ist das größte Problem war in meinem Code! – Neptune