Expand my Community achievements bar.

Don’t miss the AEM Skill Exchange in SF on Nov 14—hear from industry leaders, learn best practices, and enhance your AEM strategy with practical tips.
SOLVED

Create an Asset using HTTP API

Avatar

Level 3

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);
1 Accepted Solution

Avatar

Correct answer by
Employee Advisor

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

View solution in original post

6 Replies

Avatar

Community Advisor

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

Avatar

Community Advisor

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

Avatar

Correct answer by
Employee Advisor

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

Avatar

Level 3
ERROR* [qtp783085333-1750] org.apache.felix.http.jetty Exception while processing request to /api/assets/target-assets/new/hiking_5.jpg (org.eclipse.jetty.http.BadMessageException: 501: Unsupported Content-Encoding) org.eclipse.jetty.http.BadMessageException: 501: Unsupported Content-Encoding at org.eclipse.jetty.server.Request.extractContentParameters(Request.java:460) [org.apache.felix.http.jetty:4.0.6] at org.eclipse.jetty.server.Request.getParameters(Request.java:386) [org.apache.felix.http.jetty:4.0.6] at org.eclipse.jetty.server.Request.getParameterMap(Request.java:1039) [org.apache.felix.http.jetty:4.0.6] at javax.servlet.ServletRequestWrapper.getParameterMap(ServletRequestWrapper.java:154) [org.apache.felix.http.servlet-api:1.1.2] at org.apache.sling.engine.impl.parameters.ParameterSupport.getContainerParameters(ParameterSupport.java:346) [org.apache.sling.engine:2.6.16] at org.apache.sling.engine.impl.parameters.ParameterSupport.getRequestParameterMapInternal(ParameterSupport.java:317) [org.apache.sling.engine:2.6.16] at org.apache.sling.engine.impl.parameters.ParameterSupport.getParameter(ParameterSupport.java:179) [org.apache.sling.engine:2.6.16] at org.apache.sling.engine.impl.parameters.ParameterSupportHttpServletRequestWrapper.getParameter(ParameterSupportHttpServletRequestWrapper.java:40) [org.apache.sling.engine:2.6.16] at com.day.crx.security.token.TokenCookie.fromRequest(TokenCookie.java:98) [com.day.crx.sling.crx-auth-token:2.5.36] at com.day.crx.security.token.TokenCookie.getTokenInfo(TokenCookie.java:137) [com.day.crx.sling.crx-auth-token:2.5.36] at com.day.crx.security.token.impl.TokenAuthenticationHandler.extractCredentials(TokenAuthenticationHandler.java:490) [com.day.crx.sling.crx-auth-token:2.5.36] at org.apache.sling.auth.core.impl.AuthenticationHandlerHolder.doExtractCredentials(AuthenticationHandlerHolder.java:76) [org.apache.sling.auth.core:1.4.2]

Avatar

Employee Advisor

Hi, the problem seems to be:

Exception while processing request to /api/assets/target-assets/new/hiking_5.jpg (org.eclipse.jetty.http.BadMessageException: 501: Unsupported Content-Encoding) org.eclipse.jetty.http.BadMessageException: 501: Unsupported Content-Encoding at org.eclipse.jetty.server.Request.extractContentParameters(Request.java:460)

 

"Unsupported Content-Encoding" is definitely something caused by the request. The respective code is here: https://github.com/eclipse/jetty.project/blob/jetty-9.4.12.v20180830/jetty-server/src/main/java/org/...

 

Can you try to do the same with curl, just to find out what's the difference?