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.