PATCHing a Document using Domino Data Service

Talk about frustrating – in a week full of slow progress and CORS cross domain hell I found this little annoyance after hours of staring and curing – once again the power of trial and error triumphs over my stupidity again.

The problem

I have a Domino Data Service running on the server and I want to update a document

This is the imaginary URL

https://xomino.com/issues.nsf/api/data/documents/unid/FDCA9C28A793D3F785257C4D0068BBCA

returning the imaginary data just fine

{
    "@href":"\/issues.nsf\/api\/data\/documents\/unid\/FDCA9C28A793D3F785257C4D0068BBCA",
    "@unid":"FDCA9C28A793D3F785257C4D0068BBCA",
    "@noteid":"6EC6",
    "@created":"2013-12-26T19:03:58Z",
    "@modified":"2014-03-23T16:40:10Z",
    "@authors":
    ["CN=Marky"],
    "@form":"question",
    "MIME_Version":"1.0",
    "score":0,
    "answers":1
}

and I am trying up to update it using a simple AJAX call

$.ajax({
  data : {'marky':'newfields'},
  type: 'PATCH',	
  contentType: "application/json", 
  url:'https://xomino.com/issues.nsf/api/data/documents/unid/FDCA9C28A793D3F785257C4D0068BBCA',
  }).done(function(data){
    console.log(data)
  }).error(function(data){
    console.log(data)
  })

Unfortunately I get this big old ugly error….
{
“code”:400,
“text”:”Bad Request”,
“message”:”Error while parsing the JSON content”,
“type”:”text”,
“data”:”com.ibm.domino.services.ServiceException: Error while parsing the JSON content\r\n\tat com.ibm.domino.das.resources.DocumentResource.updateDocumentByUnid(DocumentResource.java:278)\r\n\tat com.ibm.domino.das.resources.DocumentResource.putDocumentByUnid(DocumentResource.java:184)\r\n\tat sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)\r\n\tat sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:60)\r\n\tat sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:37)\r\n\tat java.lang.reflect.Method.invoke(Method.java:611)\r\n\tat org.apache.wink.server.internal.handlers.InvokeMethodHandler.handleRequest(InvokeMethodHandler.java:63)\r\n\tat

bunch – o – useless Java Stack Trace which never helped anyone…….

com.ibm.designer.runtime.domino.adapter.LCDEnvironment.service(LCDEnvironment.java:306)\r\n\tat com.ibm.domino.xsp.bridge.http.engine.XspCmdManager.service(XspCmdManager.java:272)\r\nCaused by: com.ibm.commons.util.io.json.JsonException: Error when parsing JSON stream\r\n\tat com.ibm.commons.util.io.json.JsonParser.fromJson(JsonParser.java:86)\r\n\tat com.ibm.domino.das.resources.DocumentResource.updateDocumentByUnid(DocumentResource.java:271)\r\n\t… 65 more\r\nCaused by: com.ibm.commons.util.io.json.parser.ParseException: Encountered \” \”approved \”\” at line 1, column 1.\r\nWas expecting one of:\r\n \”false\” …\r\n \”null\” …\r\n \”true\” …\r\n <INTEGER_LITERAL> …\r\n <FLOATING_POINT_LITERAL> …\r\n <STRING_LITERAL> …\r\n \”{\” …\r\n \”[\” …\r\n \”(\” …\r\n \r\n\tat com.ibm.commons.util.io.json.parser.Json.generateParseException(Json.java:637)\r\n\tat com.ibm.commons.util.io.json.parser.Json.jj_consume_token(Json.java:572)\r\n\tat com.ibm.commons.util.io.json.parser.Json.parseJson(Json.java:409)\r\n\tat com.ibm.commons.util.io.json.JsonParser.fromJson(JsonParser.java:84)\r\n\t… 66 more\r\n”

The solution

So I googled around and I found that the error is caused because the com.ibm.commons.util.io.json.parser is expecting a string as input

http://stackoverflow.com/questions/14254536/is-there-a-way-to-return-a-json-object-within-a-xerestviewcolumn#comment19788635_14254536 – once I found that the solution was simple – send a string and not an object…

$.ajax({
  data : '{"marky":"newfields"}', // Notice this is now a string not an object
  type: 'PATCH',	
  contentType: "application/json", 
  url:'https://xomino.com/issues.nsf/api/data/documents/unid/FDCA9C28A793D3F785257C4D0068BBCA',
  }).done(function(data){
    console.log(data)
  }).error(function(data){
    console.log(data)
  })

PATCHing of a document makes a lot of sense for many reasons – mostly because of reduction in bandwidth – the example code works nicely – but you have to update the web site document to allow the PATCH method.

Be aware that using code like this you are also exposing all your data to having any old fool like me breaking your field based workflow and filling your documents full of crap data if they so chose to.

My suggestion would be to use your own REST service written with your own protections in it🙂

Once again my stupidity is boundless and as I already discovered on multiple occasions,  JSON is a string and not an object.

 

 

10 thoughts on “PATCHing a Document using Domino Data Service

  1. Thanks for clearing this up Mark. I didn’t have the time to look into these services but after this I just stick with my own rest api code.

  2. Ah one of the nice things about using AngularJS is you can just pass the scope model and it will convert to the correct data types under the covers.

    For your CORS requests did you spot you need to respond to 3 HTTP request codes? 200, 206, 204

    When I blogged about it I missed off 204 which is needed for preflight Options.

    I just wish they would sort out authentication requests probably – have an option to not return a HTTP 200 when using session based authentication, instead return 401.

    • Mark – the above was done without cross domain – the CORS preflight PATCH is what is kicking my butt right now – I get a login response without the credentials being sent – most frustrating. I will email you and then one of us can finish that blog post of yours🙂

  3. Haven’t tried this, but you might be able to use JSON.stringify on your data, something like this?

    var dataObj={ };
    dataObj.newfield=’marky’;

    $.ajax({
    data: JSON.stringify(dataObj),

    • Yes I have seen that used as an example as well – well ok I have once I started to find the answers I was looking for🙂

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s