500 error from "create case" REST API call
Having read this quick guide I've created a simple process and deployed it locally using the Bonita UI version 6.2.5 - I can login, create a new case of my process navigating to localhost:8080 in my browser. However when I attempt to create a new case using the REST api I get a HTTP 500 error and the following response returned:
To do this I'm navigating to http://localhost:8080/bonita/API/bpm/process?p=0 to obtain the process ID, which I believe is the highlighted portion of the response below:
[{"id":"7163253574217604033","icon":"","displayDescription":"","deploymentDate":"2014-04-14 16:00:54.369","description":"","activationState":"ENABLED","name":"AuthoriseBankAccountChange","deployedBy":"4","displayName":"AuthoriseBankAccountChange","last_update_date":"2014-04-14 16:00:54.553","configurationState":"RESOLVED","version":"1.0"}]
Then I'm creating the POST request as follows - I'm just putting together a quick POC\evaluation first, which is why I'm hacking a quick test together using js in my browser:
//web UI, logging out + clearing cookies, unsuccessfully navigating to
// http://localhost:8080/bonita, then issuing the command and successfully
//navigating to http://localhost:8080/bonita
var loginURL = "http://localhost:8080/bonita/loginservice"
$.post(url, { username: "walter.bates", password: "bpm", redirect: false })
// create a new case. returns http 500 error
var processURL = "http://localhost:8080/bonita/API/bpm/case/"
var processData = { "processDefinitionId": 7163253574217604033, "variables": [{"name":"claccountid", "value":"123456"},{"name":"datemodified", "value":"2014-04-14"},{"name":"modifiedby", "value":"404404"},{"name":"fieldname", "value":"SortCode"},{"name":"oldvalue" , "value":"123456"},{"name":"newvalue" , "value":"123455"},{"name":"approved","value":"false"}]}
$.post(processURL, processData)
For each of the elements in "variables" the "name" field corresponds to a variable in the main swimlane of my BPM diagram. When I change the processDefinitionId to something random I get the same error, so I'm thinking that I might be using the wrong value - is this the case?
Edit: full stack trace is as follows...
This was 100% user error and caused by my use of the jquery's $.post() which sets the contentType to application/x-www-form-urlencoded for POST data by default. I rearranged my code to use $.ajax() instead and I was able to create a new case successfully. For posterity I've included the solution below (I restarted my laptop and deployed a new instance of my process so the processDefinitionID has changed):
//web UI, logging out + clearing cookies, unsuccessfully navigating to
// http://localhost:8080/bonita, then issuing the command and successfully
//navigating to http://localhost:8080/bonita
var loginURL = "http://localhost:8080/bonita/loginservice"
$.post(loginURL, { username: "walter.bates", password: "bpm", redirect: false })
// now issue a GET to the i18ntranslation resource to set the locale
$.get("http://localhost:8080/bonita/API/system/i18ntranslation?f=locale%3Den")
// create a new case.
var processURL = "http://localhost:8080/bonita/API/bpm/case/"
var processData = { "processDefinitionId": 6481676728838404000, "variables": [{"name":"claccountid", "value":"123456"},{"name":"datemodified", "value":"2014-04-14"},{"name":"modifiedby", "value":"404404"},{"name":"fieldname", "value":"SortCode"},{"name":"oldvalue" , "value":"123456"},{"name":"newvalue" , "value":"123455"},{"name":"approved","value":"false"}]}
$.ajax({
url: processURL,
type: "POST",
dataType: "xml/html/script/json", // expected format for response
contentType: "application/json", // send as JSON
data: processData
complete: function() {
console.log("complete");
},
success: function() {
console.log("success()");
},
error: function() {
console.log("error()")
},
});
Hello,
it seems that you forget to set the language you want to use. See: http://documentation.bonitasoft.com/web-rest-api-details#authentication
Extract from the documentation:
After you log in, get the translation resource for the current locale. This is used to return the appropriate language strings in subsequent calls.
Request URL : http://host:port/bonita/API/system/i18ntranslation?f=locale%3dlocale-id Request Method : GET In the request URL, locale-id is the two-letter code identifiying the locale. For example, to get the French-language resources, use a GET request with the URL http://localhost:8080/bonita/API/system/i18ntranslation?f=locale%3dfr.
See more at: http://documentation.bonitasoft.com/web-rest-api-details#authentication
Note that this step could be not mandatory in the future ;)
Regards,
Comments
OK I may be misunderstanding the documentation but it seems to imply that I just need to issue a GET to that resource with the appropriate locale string (in my case it'd be http://localhost:8080/bonita/API/system/i18ntranslation?f=locale%3den), and then proceed with the API call. The example makes no mention of using the locale data returned - which is a giant JSON array of translated strings - which seems a little weird.
Anyway, after I do this I still get the same HTTP 500 error, so this cannot be the cause. Are you sure I am obtaining the processDefinitionId correctly? I get this same error regardless of whether or not I use a valid or invalid one.
Comments
Hi,
Could you share the stacktrace that you can find in the server log file?
Yep, just updated the question with this data