Variables for rest call from bpmn

Hi,

I’ve deployed the Dinner decision table from the get-started pages, onto an EC2 server and I want to call it from a BPMN process I’m running locally.

I looked at the example for Rest calls for calculating whether it’s a holiday on github. Where I’m stuck, is that this example didn’t have a payload. With the dinner decision table I need to pass through the number of guest and the season:-

{
  "variables": {
    "season": {"value":"Spring","type":"String"},
    "guestCount":{"value":"2","type":"String"}
  }
}

From the BPMN file I’ve got this:-

		<camunda:connector>
			<camunda:connectorId>http-connector</camunda:connectorId>
		<camunda:inputOutput>
		  
            <camunda:inputParameter name="url">
              http://ec2-xx-xx-xx-xx.eu-west-2.compute.amazonaws.com:8080/engine-rest/decision-definition/key/dish/evaluate
            </camunda:inputParameter>

            <camunda:inputParameter name="method">
              GET
            </camunda:inputParameter>

            <camunda:inputParameter name="headers">
              <camunda:map>
                <camunda:entry key="Accept">
                  application/json
                </camunda:entry>
              </camunda:map>
            </camunda:inputParameter>
			
            <camunda:inputParameter name="variables">
              <camunda:map>
                <camunda:entry key="???">
                  ???
                </camunda:entry>
              </camunda:map>
            </camunda:inputParameter>

Can someone point me to a reference for doing this in the BPMN file or just show me the format which also includes using process variables and not actually hard-coding Spring and 2 guests.

Finally, generally speaking why are all the examples in java? Isn’t the plus part of BPMN that you can change the diagram to change the process? Not a criticism, just trying to learn.

Thanks,

Anthony

@theHornet take a look at this:

When using the HTTP-Connector, you use “payload” in the connector’s input parameter.
Create a JSON string for the returned value of payload using JS or Groovy.

Can also take a look at:

the above provides a working example of a POST web service call, building a JSON payload, and parsing the response.

Edit: Also review: Evaluate Decision | docs.camunda.org, as Evaluate is a POST endpoint, but your method is currently set to GET.

1 Like

I’m confused as to what you’re asking here. Are you asking what a payload would look like to invoke the “dinner” example (I’ve worked with that also) or are you asking how to code variable inputs in the BPMN code itself?

The Dinner example outputs some things to the system/server log (essentially a deployment check to ensure that the deployed DMN is viable), but also returns a response to a REST request to the DMN when you ask to evaluate a set of inputs.

Hi mppfor_manu, Steve

I was asking for both, The dinner decision service works when I call it using chrome’s Advanced Rest Client. It’s now a case of working how how to call that decision service from a locally executed process.

I thought that I would be coding everything inside of the BPMN but Ive obviously been working with a very old version of Camuda Modeler (now rectified) and I wasn’t aware until I read Steve’s post that you can set this inside the modeler.

I was able to call the decision service using hard coded values the following way (confirmed by checking the cockpit on the amazon server)

When I tried various combinations on the example in the post to pass through the variables into the script with this:-

I wasn’t able to get through to the decision service. (BTW what does the “q” bit mean?)

I’m still working through this (slowly) so I might get some more answers (and questions) later, especially when it comes to processing the output of the decision (desiredDish).

Thanks,

Anthony

Hi Anthony,

the guest count should be integer.

Hope this helps, Ingo

Also @theHornet your life will be much happier if you build your JSON as a Js Object with dot notation, and then convert with JSON.stringify(); at the end. Much easier to maintain.

1 Like

Thanks Ingo,

That is correct as the type for guestCount is specified as an integer, however the payload i used (pasted from above) in Chrome Advanced Rest Client worked fine. The call from the local process to the cloud decision works as well (with the hard-coded payload version) as checking on the cloud cockpit I can see that it was called and that the correct output was returned.

Duly noted Stephen.

I’m working on processing the response from the decision table. From Chrome’s Advanced Rest Client for the above request I get back the following:-

[
  {
"desiredDish": {
"type": "String",
"value": "Stew",
"valueInfo": {}
}
}
],

However using the example that you supplied in your link and modifying it for this example, I used the following:-

    <camunda:outputParameter name="grub">
	<camunda:script scriptFormat="JavaScript"><![CDATA[S(response).prop("desiredDish").elements().get(0).prop("value").value();]]></camunda:script>
    </camunda:outputParameter>

But unfortunately I’m just getting the following back in my local host process when I try and call the decision

The process could not be started. :
SPIN/JACKSON-JSON-01004 Unable to find 'desiredDish'

Any pointers?

TIA

Anthony

It is because you have a array of objects. doing response.prop(desiredDish) cannot be found because desiredDish is not a property in response

https://docs.camunda.org/manual/7.6/reference/spin/json/01-reading-json/#work-with-json-arrays

If you are always getting only 1 object in your array, then just set it so you get the first item in the array. response.elements().get(0)

@theHornet review this further: https://docs.camunda.org/manual/7.6/reference/spin/json/01-reading-json/#fetch-array-of-data

1 Like

Thanks again Stephen, much appreciated.

By using response.elements().get(0) I got rid of that message, and in fact was able to load the contents of the response payload into the variable “grub”.

From the first link, I’ve tried a few combinations on top from the link you provided such as:-

var json = S(response).elements().get(0);
var customerProperty = json.prop("desiredDish");
var customers = customerProperty.elements();
var customer = customers.get(0)
var grub = customer.value();

and got the following message:-

The process could not be started. :
SPIN/JACKSON-JSON-01002 Expected 'SpinList', got 'OBJECT'

Combinations on the inline script brought back the same error number 01002. I think that I’ll be able to extract the value using an external javascript file, but it’s a pain.

Thanks again.

@theHornet you need to read the JSON Spin documents, and read up on the difference between a JSON Object and JSON array. you are using var customers = customerProperty.elements(); which is trying to get the array elements, but customerProperty is a object and so .elements() is not correct.

Should be something like:

var grub = S(response).elements().get(0).prop("desiredDish").prop("value").value();

2 Likes

Yes, that worked a treat. I’m not a programmer more of a tinkerer but I can see that I’m going to have to understand this difference between JSON objects and arrays.

I am using the parameter “var json = S(response, ‘application/json’);”, as the output of my connector, but I would like to filter the result, instead of show all the Json. Do you know you to filter the result?

This is the Json returned.
[
{
“nomeCliente”: “Andre Sousa”,
“documentoCliente”: “123456789”,
“codOrigem”: “332”,
},
{
“nomeCliente”: “Lucas Rodrigues”,
“documentoCliente”: “111111111”,
“codOrigem”: “112”,
}
]

Regards,
Rudson R.

@Rudson_Rodrigues it would be good for you to spend some time reading over the SPIN documentation:

Quering/Filtering JSON: https://docs.camunda.org/manual/7.8/reference/spin/json/03-querying-json/

Spin Docs: https://docs.camunda.org/manual/7.8/reference/spin/json/

@StephenOTT,

I got the answer in the link you mentioned.
Thanks a lot!