Accessing payload variables on server side

Hi am a newbie user of camunda . I have a question regarding accessing the payload variables that specified in the http-connector . Will those variables accessibles like as processing json data in the server side ?

Suppose in the following example am sending the post request to camunda_test.php with the payload as a json through inline script

var MyJSON = ‘{“variables”:
{“name” : {“value” : “mmk”, “type”: “String”},
“email” : {“value” : “mmk@gmail.com”, “type”: “String”}},
“businessKey” : “myBusinessKey”
}’;
execution.setVariable(“MyPayload”,MyJSON)

suppose my test.bpmn connector part looks like this

<camunda:connector>
          <camunda:inputOutput>
            <camunda:inputParameter name="url">https://192.168.1.188/camunda_test.php</camunda:inputParameter>
            <camunda:inputParameter name="method">POST</camunda:inputParameter>
            <camunda:inputParameter name="headers">
              <camunda:map>
                <camunda:entry key="accept">application/json</camunda:entry>
                <camunda:entry key="content-type">application/json</camunda:entry>
              </camunda:map>
            </camunda:inputParameter>
            <camunda:inputParameter name="payload">${MyPayload}</camunda:inputParameter>
          </camunda:inputOutput>
          <camunda:connectorId>http-connector</camunda:connectorId>
        </camunda:connector>

And in camunda_test.php can i process it like ?

$data = json_decode(file_get_contents('php://input'), true);
print_r($data);
echo $data["name"];

Is that possible or is there any workaround or easy way to do this ?

You need/should use SPIN:
var myJson = S(response);

Then set your process variable to use myJson and it will save it as Type Json

https://docs.camunda.org/manual/7.7/reference/spin/json/01-reading-json/

So att first have to set

var response = ‘{“variables”:
{“name” : {“value” : “mmk”, “type”: “String”},
“email” : {“value” : “mmk@gmail.com”, “type”: “String”}},
“businessKey” : “myBusinessKey”
}’;

and then use like

var myJson = S(response);

Right ?

take a look at:

response is a variable being returned by http-connector.

Yes i am asking about sending the process variables to the server side .Can i use plain json like

‘{“variables”:
{“name” : {“value” : “mmk”, “type”: “String”},
“email” : {“value” : “mmk@gmail.com”, “type”: “String”}},
“businessKey” : “myBusinessKey”
}’;

on the payload ? I think you are saying to use camunda spin on the receiving part .

I have given the connector with the following json in the payload as an inline Javascript

var customer_name = execution.getVariable(“customer_name ”);
var email = execution.getVariable(“email ”);
var datacenter= execution.getVariable(“datacenter”);

‘{“customer_name”:customer_name,“email”:email,“datacenter”:datacenter}’

But i can’t recieve the variables in the server side . It comes as empty . i have the following code in <?php

$data = json_decode(file_get_contents(‘php://input’), true);
//print_r($data);
//get all the request from camunda script
$var1 = $data[‘customer_name’];
$var2 = $data[‘email’];
$var4 = $data[‘datacenter’];

$array = array ($var1,$var2,$var4);
$cam_values = json_encode($array);
$myfile = fopen(“camunda_ajax_test.txt”, “w”) or die(“Unable to open file!”);
fwrite($myfile, $cam_values);
fclose($myfile);

But i can see the camunda_ajax_test.txt as

[null,null,null]

What may be the issue ?

Try this:

var myJson = {
    "customer_name": execution.getVariable('customer_name'),
    "email": execution.getVariable('email'),
    "datacenter": execution.getVariable('datacenter')
}

JSON.stringify(myJson);
2 Likes

yay , thats worked . many thanks :slight_smile:

Hi what if the response returned from a connector is a string ?
Suppose in the following reposnse

{“rows”: [
{
“name”: “Ville-Marie”,
“number”: 20
}
],
“time”: 0.003,
“fields”: {
“name”: {
“type”: “string”
},
“number”: {
“type”: “number”
}
},
“total_rows”: 1
}

Access the number field in rows (array/obj) as follows

S(response).prop(“rows”).elements().get(0).prop(“number”).numberValue();

How to access the name field in the rows (array/obj) then ?

S(response).prop(“rows”).elements().get(0).prop(“name”).stringValue(); ?

or just S(response).prop(“rows”).elements().get(0).prop(“name”).value();

Thanks , i got it …