I am trying to upload a file to the DocLibrary folder using the below method:
private static void postTheDocument() {
final String restENDPoint = "http://servername:8080/alfresco/api/-default-/public/alfresco/versions/1/nodes/48eea6b2-fe9b-4cd2-8270-5caa35d7e8dc/children";
RestTemplate restTemplate = new RestTemplate();
HttpHeaders headers = new HttpHeaders();
headers.setAccept(Arrays.asList(MediaType.APPLICATION_JSON));
headers.setBasicAuth("admin", "admin");
String requestJson = "{\"name\": \"TestName123s.doc\",\"nodeType\": \"hr:HR_Type\",\"properties\":{\"cm:title\":\"New Test title123\",\"hr:emp_no\":\"123456\",\"hr:lname\":\"Last_Name1\",\"hr:fname\":\"First_Name1\"}}";
HttpEntity<String> entity = new HttpEntity<String>(requestJson, headers);
HttpEntity<String> response = restTemplate.exchange(restENDPoint, HttpMethod.POST, entity,
String.class);
System.out.println(response.getBody().toString());
}
The documentation suggest this:
curl -utest:test -X POST host:port/alfresco/api/-default-/public/alfresco/versions/1/nodes/12341234-2344-2344-43243242334/children -F [email protected]
Using the method postTheDocument() creates the empty document. But when I try below request:
String requestJson = "{\"filedata\": \"@Z:/05test/YYYYest11qq890.pdf\",\"name\": \"TestName123s.doc\",\"nodeType\": \"hr:HR_Type\",\"properties\":{\"cm:title\":\"New Test title123\",\"hr:emp_no\":\"123456\",\"hr:lname\":\"Last_Name1\",\"hr:fname\":\"First_Name1\"}}";
I get the following Exception:
Exception in thread "main" org.springframework.web.client.HttpClientErrorException$BadRequest: 400 : [{"error":{"errorKey":"Could not read content from HTTP request body: Unrecognized field \"filedata\" (class org.alfresco.rest.api.model.Node), not marked as ignorable (36 known properties: \"modifiedB... (1969 bytes)]
What I am doing wrong here or what I am missing?
I believe the common approach is to create the node in Alfresco, then send the file to the node in a multipart upload to "http://localhost:8080/alfresco/api/-default-/public/alfresco/versions/1/nodes/-root-/children" (for an example of multipart upload see: https://www.baeldung.com/httpclient-multipart-upload).
To get the node id from the created node: