Create an Asset using HTTP API | Community
Skip to main content
debasis-swain
Level 2
Jul 10, 2020 5:03 PM UTC
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);
This post is no longer active and is closed to new replies. Need help? Start a new post to ask your question.
joerghoh
joerghohAccepted solution
Employee
Jul 10, 2020 8:21 PM UTC

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.)

6 replies

joerghoh
joerghohAccepted solution
Employee
Jul 10, 2020 8:21 PM UTC

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.)

Translate
debasis-swain
Level 2
Jul 13, 2020 10:06 AM UTC
Hi Jorg,
Translate
Vaibhavi_J
Level 7
Jul 10, 2020 7:58 PM UTC

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

Translate
Shashi_Mulugu
New Participant
Jul 10, 2020 6:02 PM UTC

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

Translate