How to update categories in Content Server 16.2 from Java REST API using Apache HttpClient

I am writing a REST client from Java using the Apache HttpClient to ContentServer 16.2. I have various functions working such as: Upload; Get; Move; Delete etc....
However, I am unable to get the Update Categories function to work from Java. I have successfully updated categories on documents via Postman.

     private static final String URL_DOCUMENT = "https://xxxxxxxxxxxxxxxxx/otcs/cs.exe/api/v2/nodes/";
     String documentNodeId = "13077";   // nodeId of the document
     String categoryNodeId = "14065";   // nodeId of category
     CloseableHttpClient httpclient = setSslHttpclient();

     HttpPut httpPut = new HttpPut(URL_DOCUMENT + documentNodeId + "/categories/" + categoryNodeId);          
     httpPut.addHeader("OTCSTicket", ticket);           

     JSONObject input = new JSONObject();  
     input.put("body", "{"14065_2":"12345678"}");           

     StringBody body = new StringBody(input.toString(), ContentType.TEXT_PLAIN);           

     HttpEntity inputEntity = MultipartEntityBuilder
                                 .create()
                                 .setContentType(ContentType.MULTIPART_FORM_DATA)
                                 //.addPart(bodyPart)
                                 .addPart("body", body)
                                 .build();              

     httpPut.setEntity(inputEntity);        
     CloseableHttpResponse response = httpclient.execute(httpPut);         

I have tried variations using FormBodyPart and setting a StringEntity with similar results.
I can recreate the same error in Postman by changing the key name from, 'body' to something else like, 'bodyx'.
Therefore, I believe that there must be something wrong with the format of the key/value pair.

{"error": "Invalid specification. Invalid attribute entry found."}

[DEBUG] 09:03:25 wire - http-outgoing-1 >> "Content-Length: 251[\r][\n]"
[DEBUG] 09:03:25 wire - http-outgoing-1 >> "Content-Type: multipart/form-data; boundary=1WMDpGFH5omBr48qjR_YKqmiVjaj--; charset=ISO-8859-1[\r][\n]"
[DEBUG] 09:03:25 wire - http-outgoing-1 >> "Host: xxxxxxxxxxxxxxxxxxx[\r][\n]"
[DEBUG] 09:03:25 wire - http-outgoing-1 >> "Connection: Keep-Alive[\r][\n]"
[DEBUG] 09:03:25 wire - http-outgoing-1 >> "User-Agent: Apache-HttpClient/4.5.3 (Java/1.7.0_60)[\r][\n]"
[DEBUG] 09:03:25 wire - http-outgoing-1 >> "Accept-Encoding: gzip,deflate[\r][\n]"
[DEBUG] 09:03:25 wire - http-outgoing-1 >> "[\r][\n]"
[DEBUG] 09:03:25 wire - http-outgoing-1 >> "--1WMDpGFH5omBr48qjR_YKqmiVjaj--[\r][\n]"
[DEBUG] 09:03:25 wire - http-outgoing-1 >> "Content-Disposition: form-data; name="body"[\r][\n]"
[DEBUG] 09:03:25 wire - http-outgoing-1 >> "Content-Type: text/plain; charset=ISO-8859-1[\r][\n]"
[DEBUG] 09:03:25 wire - http-outgoing-1 >> "Content-Transfer-Encoding: 8bit[\r][\n]"
[DEBUG] 09:03:25 wire - http-outgoing-1 >> "[\r][\n]"
[DEBUG] 09:03:25 wire - http-outgoing-1 >> "{"body":"{"14065_2":"12345678"}"}"
[DEBUG] 09:03:25 wire - http-outgoing-1 >> "[\r][\n]"
[DEBUG] 09:03:25 wire - http-outgoing-1 >> "--1WMDpGFH5omBr48qjR_YKqmiVjaj----[\r][\n]"
[DEBUG] 09:03:25 wire - http-outgoing-1 << "HTTP/1.1 500 [\r][\n]"

Successful Postman request:

Request Headers:
cache-control:"no-cache"
postman-token:"96be0138-6f42-4842-a97d-41f9fab06bef"
content-type:"multipart/form-data; boundary=--------------------------110888478679566329144486"
_

Request Body:
0:
disabled:false
description:
key:"body"
value:"{"14065_2":"555555"}"
type:"text"
_

Additonally, has anyone successfully uploaded a document and set existing category attributes in the same REST call from Java?

Tagged:

Comments

  • We figured it out. Add an extra JSON level.

         JSONObject input = new JSONObject();         
         JSONObject categories = new JSONObject();
         categories.put("14065_2", "88888888");
         categories.put("14065_3", "11111111");
    
         input.put("body", categories);