Hi everyone.
Thank for your read my topic
I’m using below API to upload file to Trados Live Cloud.
Add Source File
With Postman Application, I can upload file to end point.
But, When I create an request by MultipartUtility in my Java Application,
I got a error below
{“errorCode”:“VALIDATION_EXCEPTION”,“message”:“Invalid multipart request, either boundary missing or malformed, or missing file parameter!”,“details”:}
My source code.
private HttpURLConnection httpConn;
private DataOutputStream request;
private final String boundary = "*****";
private final String crlf = "\r\n";
private final String twoHyphens = "--";
// First part of request
public MultipartUtility(String requestURL, String token, String userId)
throws IOException {
// creates a unique boundary based on time stamp
URL url = new URL(requestURL);
httpConn = (HttpURLConnection) url.openConnection();
httpConn.setUseCaches(false);
httpConn.setDoOutput(true); // indicates POST method
httpConn.setDoInput(true);
httpConn.setRequestMethod("POST");
httpConn.setRequestProperty("X-LC-Tenant", "LC-" + userId);
httpConn.setRequestProperty("Content-Type", "multipart/form-data; boundary=" + this.boundary);
httpConn.setRequestProperty("Accept", "application/json");
httpConn.setRequestProperty("Authorization", "Bearer "+ token);
httpConn.setRequestProperty("Connection", "Keep-Alive");
System.setProperty("https.protocols", "TLSv1,TLSv1.1,TLSv1.2");
request = new DataOutputStream(httpConn.getOutputStream());
request.writeBytes(this.crlf);
}
// Add name="file" to form-data
public void addFilePart(String fieldName, File uploadFile) throws IOException {
String fileName = uploadFile.getName();
request.writeBytes(this.twoHyphens + this.boundary + this.crlf);
request.writeBytes("Content-Disposition: form-data; name=\""
+ fieldName + "\"; filename*=UTF-8''\""
+ URLEncoder.encode(fileName, "UTF-8") + "\"" + this.crlf);
request.writeBytes(this.crlf);
byte[] bytes = Files.readAllBytes(uploadFile.toPath());
request.write(bytes);
}
// Add name="properties" to form-data
public void addFormField(String name, String value) throws IOException {
request.writeBytes(this.twoHyphens + this.boundary + this.crlf);
request.writeBytes("Content-Disposition: form-data; name=\"" + name + "\"" + this.crlf);
request.writeBytes("Content-Type: text; charset=UTF-8" + this.crlf);
request.writeBytes(value + this.crlf);
request.writeBytes(this.crlf);
request.flush();
}
// End of request
public HttpURLConnection finishing() throws IOException {
request.writeBytes(this.twoHyphens + this.boundary + this.twoHyphens + this.crlf);
request.flush();
request.close();
return httpConn;
}
Please help me!