I’m trying to use the velocity plugin to access a webservice of JasperServer.
I’ve tested the call using the chrome Postman plugin and there I’m getting an response so restful is working on my JasperServer
This is the config.json that I’m using:
test_invoke:{
services: {
jasper_info:{
url:"http://localhost:8181/jasperserver-pro/rest_v2/serverInfo",
method:"get",
returns:{
data:"String"
}
}
}
And I’m calling it like this:
function onAction(event) {
plugins.Velocity.invokeService('jasper_info',{}, callBack);
}
function callBack(result){
application.output('callback');
if (result) {
if (result.exception) {
application.output(result.exception, LOGGINGLEVEL.ERROR);
} else {
application.output(JSON.stringify(result));
}
}
}
But I’m getting an empty object back from the call. maybe my config.json is incorrect, but according to the documentation it should return a string and does not require athentication.
It definitely is related to my config.json. When I remove the returns section:
test_invoke:{
services: {
jasper_info:{
url:"http://localhost:8181/jasperserver-pro/rest_v2/serverInfo",
method:"get"
}
Then I’m getting the data back in my callback function (as a json object in result.data).
How do I format my config.json so it knows it is getting a json object back?
You don’t. Actually not declaring the return in config.json will return the body of the response in result.data.
All you have to do is then convert that String, using plugins.VelocityReport.fromJSON(result.data) to get an object back.
Thanks Patrick that explains it.
Next question is how do I sent a JSON object back to the webservice in the request body?
Patrick,
This is what I came up with for the PUT request:
jasper_set_user:{
url:"http://localhost:8181/jasperserver-pro/rest_v2/organizations/$org/users/$usr",
method:"put",
authentication:"basic",
defaultUser:"superuser",
params:{
usr:"String",
org:"String",
body:"String"
},
entityFormat:"json"
},
And this is how I am calling it:
var myUser = {
fullName:"Jos de Bruijn",
emailAddress:"my@mail.address",
enabled:true,
password:'1234',
roles:[
{name:"ROLE_USER"}]
};
plugins.Velocity.invokeService('jasper_set_user',{usr: 'jos',org: 'myOrg', body: plugins.VelocityReport.toJSON(myUser)}, callBack);
And although I’m getting no error response back from the server, it has not updated my user record correctly. It has done something because I cannot login anymore with this user.
It probably is in my config.json, but I cannot find any examples that look like my situation.
If your service is expecting a JSON body you are right that you need to use ‘entityFormat: “json”’
But you should probably leave the myUser object as an Object. so
body: “Object” in config.json
and then
plugins.Velocity.invokeService(‘jasper_set_user’,{usr: ‘jos’,org: ‘myOrg’, body: myUser}, callBack);
I tried that, but then I get the following reply:
net.stuff.servoy.plugin.velocityreport.exception.UnknownParameterTypeException: Unknow type: body
Hum, I need to check that out. What happens if you don’t declare it at all (but pass it in the params)?
It also really depends on the type of structure the service is expecting.
Receiving { body: { prop: “value” } } is different from receiving {prop: “value”} so that part needs to be check in the service documentation.
Not declaring it does not work.
i’ve tested the webservice with the postman plugin in chrome.
In there I’ve added 2 headers: Authentication and content-type: application/json
And in the raw field i’ve added the JSON string. That is accepted by the webservice, so I know that is working.
The webservice returns the changed record as xml string.
When I run from velocity I do get an xml string back, but it has no changed data. So the webservice looks like it is accepting my data (I dont get an error), but not really doing anything with it.
Sorry for the delay on this, had been quite busy lately…
There was an issue when the processType was set to “server”, the result from the service invocation was an empty object if you didn’t specify the returns property.
I’ve fixed that in v3.5.5 that I just released. I also added the option to specify the type of return value as “xml” or “json”,
so you can declare:
returns: {
data: “json”
}
or
return: {
data: “xml”
}
And you won’t need to deserialize the result. It will be received as a Servoy JavaScript object with the name you’ve specified.
As to why your service is not updating the data, I would have to know the service and what it expects to be able to help.
What you can do is change the url of your service to point to a request bin url (you can create one here: http://requestb.in/). This will allow you to inspect what is sent to invoke your service and see if the data (and headers) matches your service expectations…
Hope this helps.