Executing Server Code

See the below example for executing a server code from a thing.

/* Set server code parameters. */
#define ENDPOINT_NAME "myFunction"
#define PARAMS "{\"arg1\":\"abc\", \"arg2\":123}"

int ret;

/* Execute server code. */
ret = kii_server_code_execute(&kii, ENDPOINT_NAME, PARAMS);
if (ret != 0) {
  /* Handle the error. */
  return;
}

printf("returned value:%s\n", kii.kii_core.response_body);

In the above example, the endpoint of server code, myFunction is called.

Specify parameters in JSON format. In the above example, the params, which is the first argument of the myFunction, is called with the parameters params.arg1 and params.arg2, with the values abc and 123, respectively. See Server Code Syntax for more information about arguments for server code.

You receive the return value from the server code as a JSON string. When the function succeeds, the kii.kii_core.response_body contains a JSON string as below. You can use the kiijson library to get the value of a specific key within the JSON string. See <a href="/en/guides/thingifsdk/nontrait/thing/json-library/">Parsing JSON to learn how to implement it.

{"returnedValue":"success"}

The above example is a return value of the below server code which executes the done function with the argument success.

function myFunction(params, context, done) {
  done("success");
}