Script Callout
The Script Callout policy enables addition of custom JavaScript codes and Python codes that execute within the context of an API proxy flow.
Configuration
This policy can manage the function of Assign Variables, allowing the configuration of multiple context variables in the script, that is, addition or replacement/modification of payload and Context Variables.
As JavaScript is a single-threaded, non-blocking language, please avoid using Javascript as the source for those APIs that handle concurrent requests or dynamic message content.
Example
A basic script is provided for javascript and python in the API UI itself (as seen in the figure above), which acts as a template. Below are sample scripts available in the Script editor section.
JavaScript
// function definition
function myFunction() {
message.payload = '<Modified Payload >';
message.contextVariable.'<context variable name>' = '<value of modified context variable>';
}
// function invocation
myFunction();
Python
def myFunction():
if(message.has_key('payload')):
message['payload'] ='<modified payload >'
if(message['contextVariable'].has_key('<context variable name>')):
message['contextVariable']['<context variable name>'] = '<Value of context variable>'
return message;