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
  • Hi Everybody.

    I resolved it by my self.

    this is my code, that request to end point.

    ======================

    public static ResponseModelBase doUploadFile(String url, String token, String userId,File file, Map<String, String> fields, Class responseSuccessType)throws IOException, Exception {
    //Creating the FileBody object
    FileBody filebody = new FileBody(file, ContentType.MULTIPART_FORM_DATA, URLEncoder.encode(file.getName(), "UTF-8"));

    //Creating the MultipartEntityBuilder
    MultipartEntityBuilder entitybuilder = MultipartEntityBuilder.create();
    entitybuilder.setMode(HttpMultipartMode.BROWSER_COMPATIBLE);

    for (Map.Entry<String, String> entry : fields.entrySet()) {
    String key = entry.getKey();
    String value = entry.getValue();
    entitybuilder.addTextBody(key, value);
    }

    entitybuilder.addPart("file", filebody);
    HttpEntity mutiPartHttpEntity = entitybuilder.build();

    HttpPost httpPost = new HttpPost(url);
    httpPost.setEntity(mutiPartHttpEntity);
    httpPost.setHeader("Authorization", "Bearer " + token);
    httpPost.setHeader("X-LC-Tenant", "LC-" + userId);

    //Creating CloseableHttpClient object
    CloseableHttpClient httpClient = HttpClients.createDefault();
    HttpResponse response = null;
    try {
    response = httpClient.execute(httpPost);
    return getResponseFile(response, responseSuccessType, responseSuccessType);
    } catch (IOException e) {
    e.printStackTrace();
    return null;
    }
    }
    =======================

     

    emoji
  • Hi ,

    The investigation was in progress with our team and I'm happy to see that you fixed the code and that you can continue with your integration.

    emoji
Reply Children