how to set the payload using javascript callout?

i would like to extract 2 variables from the response using extract variables and set the content of the payload using javascript callout.

I need name and client id in my payload. @Anil Sagar @ Google

7756-extractvariables.png

my response payload is

{ "appId":"f9ea6c2d-019f-4478-xxxx-b80852844c4c", "attributes":[], "createdAt":1543446822040, "createdBy":"xxxxx@outlook.com", "credentials":[ { "apiProducts":[ { "apiproduct":"xxx", "status":"approved" } ], "attributes":[ ], "consumerKey":"xxxxxxxiqfU79At9U6AlVePlvyM9pzG", "consumerSecret":"SxxxxxxUItjr", "expiresAt":1546038822130, "issuedAt":1543446822130, "scopes":[ ], "status":"approved"

0 1 440
1 REPLY 1

jbradley
Participant I

Your ExtractVariables step above, is setting 2 variables with your content:

  • clientId
  • appName

The relevant information regarding the Javascript Object model is here. However, this is one way you could do it:

var client_id = context.getVariable('clientID') || '';
var name = context.getVariable('appName') || '';
var message_str = context.getVariable('message.content'); // This will return the message content regardless of context
var message_obj = {};
try {
  message_obj = JSON.parse(message_str);
} catch (_er) {
  print('Could not parse message to an object!');
}

if (message_obj) {
  message_obj.client_id = client_id;
  message_obj.name = name;
  context.setVariable('message.content', JSON.stringify(message_obj));
}