Problems adding tags to documents using webscript API
Options
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
05-05-2012 05:51 PM
I'm trying to add tags to documents in Alfresco using Alfresco's webscript API.
I manage to read the tags from documents using GET but when I try to POST to add a tag I get response code 500 from Alfresco and an error message in the Tomcat log including "…Expression tags is undefined…".
I guest there is something wrong with the way I send the arguments to the POST.
Has anyone managed to do this?
The Groovy code I use is:
import java.net.URL;
import java.net.URLConnection;
import java.net.URLEncoder;
import org.apache.commons.codec.binary.Base64;
def urlString = "http://127.0.0.1:8080/alfresco/service/api/node/workspace/SpacesStore/47341837-0f28-4fb4-a7a1-a1a65b...";
URLEncoder e = new URLEncoder();
def queryString = e.encode('tags=["tag1","tag2"]');
def url = new URL(urlString);
URLConnection connection = url.openConnection();
String authString = "admin" + ":" + "admin";
String authStringEnc = new String(Base64.encodeBase64(authString.getBytes()));
connection.setRequestMethod("POST");
connection.setRequestProperty("Authorization", "Basic " + authStringEnc);
connection.doOutput = true;
def writer = new OutputStreamWriter(connection.outputStream);
writer.write(queryString);
writer.flush();
writer.close();
connection.connect();
return connection.content.text;
I manage to read the tags from documents using GET but when I try to POST to add a tag I get response code 500 from Alfresco and an error message in the Tomcat log including "…Expression tags is undefined…".
I guest there is something wrong with the way I send the arguments to the POST.
Has anyone managed to do this?
The Groovy code I use is:
import java.net.URL;
import java.net.URLConnection;
import java.net.URLEncoder;
import org.apache.commons.codec.binary.Base64;
def urlString = "http://127.0.0.1:8080/alfresco/service/api/node/workspace/SpacesStore/47341837-0f28-4fb4-a7a1-a1a65b...";
URLEncoder e = new URLEncoder();
def queryString = e.encode('tags=["tag1","tag2"]');
def url = new URL(urlString);
URLConnection connection = url.openConnection();
String authString = "admin" + ":" + "admin";
String authStringEnc = new String(Base64.encodeBase64(authString.getBytes()));
connection.setRequestMethod("POST");
connection.setRequestProperty("Authorization", "Basic " + authStringEnc);
connection.doOutput = true;
def writer = new OutputStreamWriter(connection.outputStream);
writer.write(queryString);
writer.flush();
writer.close();
connection.connect();
return connection.content.text;
Labels:
- Labels:
-
Archive
1 REPLY 1
Options
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
06-22-2012 07:29 PM
I think the problem is that you are posting the tags as a query string instead of posting JSON in the body of the request.
Here's how you'd do it in curl:
curl -X POST -uadmin:admin "http://127.0.0.1:8080/alfresco/service/api/node/workspace/SpacesStore/637632bf-b633-472b-8696-a62e74..." -H "content-type: application/json" -d @/Users/jpotts/Documents/code/tags.json
And then in tags.json, you'd have:
["tag1","tag2"]
Also note that the content-type in the header needs to be set to application/json.
Jeff
Here's how you'd do it in curl:
curl -X POST -uadmin:admin "http://127.0.0.1:8080/alfresco/service/api/node/workspace/SpacesStore/637632bf-b633-472b-8696-a62e74..." -H "content-type: application/json" -d @/Users/jpotts/Documents/code/tags.json
And then in tags.json, you'd have:
["tag1","tag2"]
Also note that the content-type in the header needs to be set to application/json.
Jeff
