Search code examples
spring-bootdocushare

the request was rejected because no multipart boundary was found when creating new document in Docushare Flex


I am trying to create new document in Docushare Flex using new docushare rest api and my request body suppose to be XML and I am generating it with requested data, when I send the request I get this error "org.apache.commons.fileupload.FileUploadException: the request was rejected because no multipart boundary was found"

    HttpPost request = new HttpPost(postUrl);
    String filePath = "C:/Test/CreateDocument.xml";
    String createObj =  helper.createDocumentXml(filePath, parentId, documentTitle, fileName, ownerId);
    String createDocumentXml= null;
    {
        try {
            createDocumentXml = FileUtils.readFileToString(new File(filePath));
        } catch (IOException e) {
            e.printStackTrace();
        }
    }
    StringEntity bodyEntity = new StringEntity(createDocumentXml, ContentType.MULTIPART_FORM_DATA);
    request.setEntity(bodyEntity);

    CloseableHttpResponse response =  client.execute(request);
    System.out.println("Status is " +  response.getStatusLine());
    HttpEntity entity = response.getEntity();

Solution

  • I have used this block of code to upload a document in DocuShare Flex

    HttpPost request = new HttpPost(postUrl);
    String filePath = "C:/Test/CreateDocument.xml";
    String createObj =  helper.createDocumentXml();
    String createDocumentXml= null;
    {
        try {
            createDocumentXml = FileUtils.readFileToString(new File(filePath));
        } catch (IOException e) {
            e.printStackTrace();
        }
    }
        FileBody body = new FileBody(file.toFile());
    
        StringBody xmlContent = new StringBody(createDocumentXml, ContentType.APPLICATION_XML);
        String boundry = UUID.nameUUIDFromBytes(file.toString().getBytes()).toString();
        HttpEntity entity = MultipartEntityBuilder.create()
                .setBoundary(boundry)
                .setCharset(Charset.forName("UTF-8"))
                .setMode(HttpMultipartMode.BROWSER_COMPATIBLE)`enter code here`
                .addPart("content", body)
                .addPart("request", xmlContent)
                .build();
    
        request.setEntity(entity);
    
        CloseableHttpResponse response =  client.execute(request);
        HttpEntity entity = response.getEntity();