Create an Asset using HTTP API

Avatar

Avatar
Validate 1
Level 2
debasis-swain
Level 2

Likes

12 likes

Total Posts

23 posts

Correct reply

2 solutions
Top badges earned
Validate 1
Ignite 1
Give Back
Boost 5
Boost 3
View profile

Avatar
Validate 1
Level 2
debasis-swain
Level 2

Likes

12 likes

Total Posts

23 posts

Correct reply

2 solutions
Top badges earned
Validate 1
Ignite 1
Give Back
Boost 5
Boost 3
View profile
debasis-swain
Level 2

10-07-2020

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

Accepted Solutions (1)

Accepted Solutions (1)

Avatar

Avatar
Coach
Employee
Jörg_Hoh
Employee

Likes

1,136 likes

Total Posts

3,167 posts

Correct reply

1,080 solutions
Top badges earned
Coach
Give back 600
Ignite 5
Ignite 3
Ignite 1
View profile

Avatar
Coach
Employee
Jörg_Hoh
Employee

Likes

1,136 likes

Total Posts

3,167 posts

Correct reply

1,080 solutions
Top badges earned
Coach
Give back 600
Ignite 5
Ignite 3
Ignite 1
View profile
Jörg_Hoh
Employee

10-07-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
Hi Jorg,
debasis-swain
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]
Jörg_Hoh

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?

 

Answers (2)

Answers (2)

Avatar

Avatar
Affirm 50
MVP
Vaibhavi
MVP

Likes

216 likes

Total Posts

181 posts

Correct reply

61 solutions
Top badges earned
Affirm 50
Validate 1
Ignite 1
Give Back 5
Give Back 3
View profile

Avatar
Affirm 50
MVP
Vaibhavi
MVP

Likes

216 likes

Total Posts

181 posts

Correct reply

61 solutions
Top badges earned
Affirm 50
Validate 1
Ignite 1
Give Back 5
Give Back 3
View profile
Vaibhavi
MVP

10-07-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

Avatar

Avatar
Springboard
MVP
Shashi_Mulugu
MVP

Likes

233 likes

Total Posts

295 posts

Correct reply

67 solutions
Top badges earned
Springboard
Bedrock
Validate 1
Applaud 100
Establish
View profile

Avatar
Springboard
MVP
Shashi_Mulugu
MVP

Likes

233 likes

Total Posts

295 posts

Correct reply

67 solutions
Top badges earned
Springboard
Bedrock
Validate 1
Applaud 100
Establish
View profile
Shashi_Mulugu
MVP

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