Skip to main content
debasis-swain
Level 2
July 10, 2020
Solved

Create an Asset using HTTP API

  • July 10, 2020
  • 3 replies
  • 2572 views

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);
This post is no longer active and is closed to new replies. Need help? Start a new post to ask your question.
Best answer by joerghoh

There must be a log entry (most likely an exception) in the error.log, which gives more information about this statuscode 500. Can you share that exception?

 

(Very unlikely that the CSRF filter is causing this. Because a few user agents are explicitly listed, which are ignored by the filter.)

3 replies

Shashi_Mulugu
Community Advisor
Community Advisor
July 10, 2020

Please refer to the below community post. It could because of anonymous post request. Either remove post from sling referee filter or add csrf header..

 

https://experienceleaguecommunities.adobe.com/t5/adobe-experience-manager/httppost-to-aem-6-4-author-instance-using-assets-api-through/qaq-p/362162

Vaibhavi_J
Level 7
July 10, 2020

Hi @debasis-swain , 

Have you met the prerequisite to use asset API?

Please follow the below step. 

Prerequisites
  • Access https://[aem_server]:[port]/system/console/configMgr .
  • Navigate to Adobe Granite CSRF Filter .
  • Make sure the property Filter Methods includes: POST , PUT , DELETE

Below document details about asset API in detail. Also possible error scenarios

joerghoh
Adobe Employee
joerghohAdobe EmployeeAccepted solution
Adobe Employee
July 10, 2020

There must be a log entry (most likely an exception) in the error.log, which gives more information about this statuscode 500. Can you share that exception?

 

(Very unlikely that the CSRF filter is causing this. Because a few user agents are explicitly listed, which are ignored by the filter.)

debasis-swain
Level 2
July 13, 2020
Hi Jorg,