How to create a resource mapping node in AEM using sling REST api and curl

906 views Asked by At

I'm trying to create a node using the sling api in AEM 6.0. Now I know I can easily create a node using a curl call like this

curl -u admin:admin -F"jcr:primaryType= sling:Mapping" http://localhost:4502/content/mynode

But what if I need to get fancy and try to create a resource mapping node with a name like this /etc/map.publish.prd/http/prd.rb.mysite.ca

Now the following curl call does not work anymore because part of the url is treated as selectors

curl -u admin:admin -F"jcr:primaryType= sling:Mapping"  http://localhost:4502/etc/map.publish.prd/http/prd.rb.mysite.com

So how would I go about avoiding this issue?

I also tried using the name property to limit the complexity of the url like this.

curl -u admin:admin -F"jcr:primaryType=sling:Mapping" -F"name=prd.rb.mysite.com" http://localhost:4502/etc/map.publish.prd/http

But I think sling still gets confused by map.publish.prd

Any help will be much appreciated Thanks

-Alain

2

There are 2 answers

0
Alain Laniel On BEST ANSWER

Here is the workaround we found. The script uses a first call to create the node under /tmp like this

curl -X POST -u admin:admin -F"jcr:primaryType=sling:Mapping" http://localhost:4502/tmp/my-temp-site.

From there the script can add additional nodes under my-temp-site as needed with more curl calls. When the script is done tinkering with the my-temp-site node it then moves it to it's final resting place with this

curl -X POST -u admin:admin -F":operation=move" -F":dest=/etc/map.publish.prd/http/prd.rb.mysite.ca" http://localhost:4502/tmp/my-tmp-site

We've tested it and so far it works us.

Like @bertrand mentioned we will add a timestamp to the name of the /tmp/my-temp-site node to make it unique and avoid conflicts

-Alain

5
Bertrand Delacretaz On

The trick is to post to /etc/* which causes the :name parameter to be used as is for the new node's path:

  curl -u admin:admin \
  -F"jcr:primaryType=sling:Mapping" \
  -F:name=./map.publish.prd/http/prd.rb.mysite.ca \
  http://localhost:4502/etc/*

This only works if /etc/map.publish.prd does not exist yet, but otherwise you can use the same trick further down the tree.