Solved
Create an Asset using HTTP API
How to use the HTTP API to create an asset in an AEM instance. I am using the below code and getting a 500 Server error.
CredentialsProvider credentialsProvider = new BasicCredentialsProvider();
credentialsProvider.setCredentials(
new AuthScope(AuthScope.ANY_HOST, AuthScope.ANY_PORT),
new UsernamePasswordCredentials("admin", "admin"));
CloseableHttpClient httpClient = HttpClients.custom().setDefaultCredentialsProvider(credentialsProvider).build();
CloseableHttpResponse response = null;
try {
java.net.URI uri = new URIBuilder()
.setScheme("http")
.setHost("localhost:4506")
.setPath("/api/assets/target-assets/new/" + asset.getName())
.build();
log.info("The post request URI is {}", uri.toString());
HttpPost postRequest = new HttpPost(uri);
postRequest.addHeader("content-type", asset.getMimeType());
InputStreamEntity entity = new InputStreamEntity(asset.getOriginal().getStream());
entity.setContentEncoding("binary/octet-stream");
entity.setChunked(true);
postRequest.setEntity(new BufferedHttpEntity(entity));
response = httpClient.execute(postRequest);