cancel
Showing results for 
Search instead for 
Did you mean: 

creating web assets with WCM REST API

kb9zzw
Champ in-the-making
Champ in-the-making
I'm trying to figure out how to create / upload a file in a WCM sandbox using the WCM REST API for Alfresco 3.2.  I'm using PHP/cURL as my client.  I've been able to create an asset using a JSON post to the following API:

Create a new WCM asset.
POST /alfresco/service/api/wcm/webprojects/{webprojectref}/sandboxes/{sandboxref}/assets/{path}

However, I'm at a loss to figure out how to upload a file instead of dropping string content into the "content" JSON field.  Documentation suggests the following for the "content" input value:

content:
    optional, string content of the new file, this is a convenience method, normally content is added via the file upload which gives greater control over the content.

I'm at a loss to understand what file upload mechanism is needed.  Perhaps someone can look at my sample code and suggest how this might be changed to accommodate a file upload?

Sample PHP / cURL code:

<?php

# Web project and sandbox
$webproject="test";
$sandbox="test–test.user";

# Define Asset
$path = "www/avm_webapps/ROOT/";
$asset = '{ name:"sample.txt",type:"file",content:"this is a sample text file" }';

# Service API
$api = "http://localhost:8080/alfresco/service/api/wcm/webprojects/$webproject/sandboxes/$sandbox/assets/$pa...";

# Create asset
$ch = curl_init();
curl_setopt($ch, CURLOPT_URL, $api);
curl_setopt($ch, CURLOPT_USERPWD,"username:password");
curl_setopt($ch,CURLOPT_RETURNTRANSFER,1);
curl_setopt($ch,CURLOPT_HTTPHEADER, array("Content-Type:application/json"));
curl_setopt($ch, CURLOPT_POST, 1);
curl_setopt($ch, CURLOPT_POSTFIELDS, $asset);

$data = curl_exec($ch);
curl_close($ch);

var_dump($data);

?>

Thanks

-Jon
4 REPLIES 4

pconnors
Champ in-the-making
Champ in-the-making
I'm attempting to do the same thing.  I get a file uploaded using the sample/upload scripts, but unfortunately it only puts the file into the companyhome space.  I can't figure out how to get the file into a sandbox to then get submit and deployed.  The webscript you're using seems to only to content strings, but unfortunately I can't even get that to work with my installation.  So I'm stuck with my files in Company Home with no way to get them into a valid Web Project Sandbox.  Anyone else have a working installation of this?  I can't imagine there's no one out there doing this?

Thanks a bunch,

pete

kb9zzw
Champ in-the-making
Champ in-the-making
Pete,

You might try using the REST login / logout API to get a token instead of the cURL basic auth.  Also, ensure that the user has a content contributor role in the web project.  Finally, double check your path and make sure you are uploading to the correct sandbox.

You are right, however, that the "create asset" API only accepts a content string.  I still haven't figured out how one would upload a file (i.e. image) using the REST API into a WCM sandbox.  If anyone has a suggestion, I would be much obliged.

-Jon

darklord
Champ in-the-making
Champ in-the-making
Hi Friends,

I am trying to create a file instead of upload and having some problem. I am calling create asset call via java code to create content but seeing some exception. Please see my code and exceptions below…



                         HttpClient client = new HttpClient();
         
         
         String asset = "'{ name:\"specialfile.txt\",type:\"file\",content:\"this is a sample text file\" }'";
         
         String URL = "http://localhost:8080/alfresco/service/api/wcm/webprojects/myproject/sandboxes/myproject--admin/asse...";
         
               PostMethod post = new PostMethod(URL);
         NameValuePair[] data = {new NameValuePair("asset", asset)};
            
         client.getState().setCredentials(
                   new AuthScope("localhost", 8080, null),
                   new UsernamePasswordCredentials("admin", "admin")
                   );
         post.setRequestBody(data);
         int statusCode1 = client.executeMethod(post);
         

And this is the exception i see in the tomcat logs…


19:08:24,010 User:admin ERROR [freemarker.runtime] Error executing macro: assetJSON
required parameter: asset is not specified.

Error executing macro: assetJSON
required parameter: asset is not specified.
The problematic instruction:
———-
==> macro assetJSON [on line 1, column 1 in org/alfresco/repository/wcm/sandbox/Asset/asset.lib.ftl]
in user-directive assetLib.assetJSON [on line 3, column 9 in org/alfresco/repository/wcm/sandbox/Asset/asset.post.json.ftl]
———-

Java backtrace for programmers:
———-
freemarker.template.TemplateException: Error executing macro: assetJSON
required parameter: asset is not specified.
        at freemarker.core.Macro$Context.sanityCheck(Macro.java:211)
        at freemarker.core.Macro$Context.runMacro(Macro.java:169)
        at freemarker.core.Environment.visit(Environment.java:603)
        at freemarker.core.UnifiedCall.accept(UnifiedCall.java:106)
        at freemarker.core.Environment.visit(Environment.java:210)
        at freemarker.core.MixedContent.accept(MixedContent.java:92)
        at freemarker.core.Environment.visit(Environment.java:210)
        at freemarker.core.Environment.process(Environment.java:190)
        at freemarker.template.Template.process(Template.java:237)
        at org.alfresco.repo.template.FreeMarkerProcessor.process(FreeMarkerProcessor.java:197)
        at org.springframework.extensions.webscripts.AbstractWebScript.renderTemplate(AbstractWebScript.java:580)
        at org.springframework.extensions.webscripts.DeclarativeWebScript.renderFormatTemplate(DeclarativeWebScript.java:267)
        at org.springframework.extensions.webscripts.DeclarativeWebScript.execute(DeclarativeWebScript.java:147)
        at org.alfresco.repo.web.scripts.RepositoryContainer$2.execute(RepositoryContainer.java:383)
        at org.alfresco.repo.transaction.RetryingTransactionHelper.doInTransaction(RetryingTransactionHelper.java:381)
        at org.alfresco.repo.web.scripts.RepositoryContainer.transactionedExecute(RepositoryContainer.java:436)
        at org.alfresco.repo.web.scripts.RepositoryContainer.transactionedExecuteAs(RepositoryContainer.java:466)
        at org.alfresco.repo.web.scripts.RepositoryContainer.executeScript(RepositoryContainer.java:304)
        at org.springframework.extensions.webscripts.AbstractRuntime.executeScript(AbstractRuntime.java:333)
        at org.springframework.extensions.webscripts.AbstractRuntime.executeScript(AbstractRuntime.java:189)
        at org.springframework.extensions.webscripts.servlet.WebScriptServlet.service(WebScriptServlet.java:118)
        at javax.servlet.http.HttpServlet.service(HttpServlet.java:717)
        at org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:290)
        at org.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationFilterChain.java:206)
        at org.alfresco.web.app.servlet.GlobalLocalizationFilter.doFilter(GlobalLocalizationFilter.java:58)
        at org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:235)
        at org.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationFilterChain.java:206)
        at org.apache.catalina.core.StandardWrapperValve.invoke(StandardWrapperValve.java:233)
        at org.apache.catalina.core.StandardContextValve.invoke(StandardContextValve.java:191)
        at org.apache.catalina.core.StandardHostValve.invoke(StandardHostValve.java:127)
        at org.apache.catalina.valves.ErrorReportValve.invoke(ErrorReportValve.java:102)
        at org.apache.catalina.valves.AccessLogValve.invoke(AccessLogValve.java:555)
        at org.apache.catalina.core.StandardEngineValve.invoke(StandardEngineValve.java:109)
        at org.apache.catalina.connector.CoyoteAdapter.service(CoyoteAdapter.java:298)
        at org.apache.coyote.http11.Http11Processor.process(Http11Processor.java:852)
        at org.apache.coyote.http11.Http11Protocol$Http11ConnectionHandler.process(Http11Protocol.java:588)
        at org.apache.tomcat.util.net.JIoEndpoint$Worker.run(JIoEndpoint.java:489)
        at java.lang.Thread.run(Thread.java:619)
19:08:24,015 User:admin ERROR [extensions.webscripts.AbstractRuntime] Exception from executeScript - redirecting to status template error: 02130028 Wrapped Exception (with status template): 02130034 Error during processing of the template 'Error executing macro: assetJSON
required parameter: asset is not specified.'. Please contact your system administrator.
org.springframework.extensions.webscripts.WebScriptException: 02130028 Wrapped Exception (with status template): 02130034 Error during processing of the template 'Error executing macro: assetJSON
required parameter: asset is not specified.'. Please contact your system administrator.
        at org.springframework.extensions.webscripts.AbstractWebScript.createStatusException(AbstractWebScript.java:749)
        at org.springframework.extensions.webscripts.DeclarativeWebScript.execute(DeclarativeWebScript.java:171)
        at org.alfresco.repo.web.scripts.RepositoryContainer$2.execute(RepositoryContainer.java:383)
        at org.alfresco.repo.transaction.RetryingTransactionHelper.doInTransaction(RetryingTransactionHelper.java:381)
        at org.alfresco.repo.web.scripts.RepositoryContainer.transactionedExecute(RepositoryContainer.java:436)
        at org.alfresco.repo.web.scripts.RepositoryContainer.transactionedExecuteAs(RepositoryContainer.java:466)
        at org.alfresco.repo.web.scripts.RepositoryContainer.executeScript(RepositoryContainer.java:304)
        at org.springframework.extensions.webscripts.AbstractRuntime.executeScript(AbstractRuntime.java:333)
        at org.springframework.extensions.webscripts.AbstractRuntime.executeScript(AbstractRuntime.java:189)
        at org.springframework.extensions.webscripts.servlet.WebScriptServlet.service(WebScriptServlet.java:118)
        at javax.servlet.http.HttpServlet.service(HttpServlet.java:717)
        at org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:290)
        at org.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationFilterChain.java:206)
        at org.alfresco.web.app.servlet.GlobalLocalizationFilter.doFilter(GlobalLocalizationFilter.java:58)
        at org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:235)
        at org.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationFilterChain.java:206)
        at org.apache.catalina.core.StandardWrapperValve.invoke(StandardWrapperValve.java:233)
        at org.apache.catalina.core.StandardContextValve.invoke(StandardContextValve.java:191)
        at org.apache.catalina.core.StandardHostValve.invoke(StandardHostValve.java:127)
        at org.apache.catalina.valves.ErrorReportValve.invoke(ErrorReportValve.java:102)
        at org.apache.catalina.valves.AccessLogValve.invoke(AccessLogValve.java:555)
        at org.apache.catalina.core.StandardEngineValve.invoke(StandardEngineValve.java:109)
        at org.apache.catalina.connector.CoyoteAdapter.service(CoyoteAdapter.java:298)
        at org.apache.coyote.http11.Http11Processor.process(Http11Processor.java:852)
        at org.apache.coyote.http11.Http11Protocol$Http11ConnectionHandler.process(Http11Protocol.java:588)
        at org.apache.tomcat.util.net.JIoEndpoint$Worker.run(JIoEndpoint.java:489)
        at java.lang.Thread.run(Thread.java:619)
Caused by: org.alfresco.service.cmr.repository.TemplateException: 02130034 Error during processing of the template 'Error executing macro: assetJSON
required parameter: asset is not specified.'. Please contact your system administrator.
        at org.alfresco.repo.template.FreeMarkerProcessor.process(FreeMarkerProcessor.java:201)
        at org.springframework.extensions.webscripts.AbstractWebScript.renderTemplate(AbstractWebScript.java:580)
        at org.springframework.extensions.webscripts.DeclarativeWebScript.renderFormatTemplate(DeclarativeWebScript.java:267)
        at org.springframework.extensions.webscripts.DeclarativeWebScript.execute(DeclarativeWebScript.java:147)
        … 25 more
Caused by: freemarker.template.TemplateException: Error executing macro: assetJSON
required parameter: asset is not specified.
        at freemarker.core.Macro$Context.sanityCheck(Macro.java:211)
        at freemarker.core.Macro$Context.runMacro(Macro.java:169)
        at freemarker.core.Environment.visit(Environment.java:603)
        at freemarker.core.UnifiedCall.accept(UnifiedCall.java:106)
        at freemarker.core.Environment.visit(Environment.java:210)
        at freemarker.core.MixedContent.accept(MixedContent.java:92)
        at freemarker.core.Environment.visit(Environment.java:210)
        at freemarker.core.Environment.process(Environment.java:190)
        at freemarker.template.Template.process(Template.java:237)
        at org.alfresco.repo.template.FreeMarkerProcessor.process(FreeMarkerProcessor.java:197)
        … 28 more

bigobigo
Champ in-the-making
Champ in-the-making
Do you need a solution for this ?

Here is the solution for creating asset and uploading it's content with UploadServlet:

https://forums.alfresco.com/en/viewtopic.php?f=30&t=41840

Regards Luke.