Add Source File To Exist Project Error "VALIDATION_EXCEPTION"

Hi Everyone.

I got trouble when using API to upload source file to Exist Project.

I'm using below API to upload source file.

RWS Language Cloud API Documentation (sdl.com)

My source to create request.

==========================================================
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;
}
==========================================================

BUT, I get below error.

{"errorCode":"VALIDATION_EXCEPTION","message":"Invalid multipart request, either boundary missing or malformed, or missing file parameter!","details":[]}

Please tell me, What wrong with abow code. Please

emoji
Parents Reply Children