Uploading file using the REST API with HttpURLConnection

cancel
Showing results for 
Search instead for 
Did you mean: 
aturbati
Active Member

Uploading file using the REST API with HttpURLConnection

Hi, I'm using the acs-deployment 7.2.0 (the docker deployment) and I would like to upload a file using the REST APIs with Java HttpURLConnection, so by explicitly doing the POST (at the moment I cannot use the ReST API Java Wrapper Extension Point). From https://docs.alfresco.com/content-services/latest/develop/rest-api-guide/folders-files/#uploadfile I see the curl example, but I could not "translate" it in Java (I had no problem with the POST using the alfresco/api/-default-/public/authentication/versions/1/tickets call). I do not know how to deal with the various "-F" parameters and on how to pass the content of the file itself.

Has anyone use the direct REST APIs to upload a file in ACS? Could you please provide me a simple example on how to do it?

Thank you

Andrea

2 Replies
angelborroy
Alfresco Employee

Re: Uploading file using the REST API with HttpURLConnection

aturbati
Active Member

Re: Uploading file using the REST API with HttpURLConnection

angelborroy thank you for the response.

Following the link you provided, I was able to make it work with CloseableHttpClient + HttpPost + MultipartEntityBuilder classes. I'm still not able to make it work with the HttpURLConnection (I get a 400 response), but at least I know it can work with plain Java calls using the REST APIs.

The working code (so it could be useful for anyone who has the same issue) is:

CloseableHttpClient httpClient = HttpClients.createDefault();
HttpPost uploadFile = new HttpPost("http://localhost:8080/alfresco/api/-default-/public/alfresco/versions/1/nodes/-shared-/children");
uploadFile.addHeader("Authorization", bacisAutString);

MultipartEntityBuilder builder = MultipartEntityBuilder.create();
FileBody fileBody = new FileBody(new File(filePath));
builder.addPart("filedata", fileBody);

HttpEntity multipart = builder.build();
uploadFile.setEntity(multipart);
HttpResponse response = httpClient.execute(uploadFile);