How to pass the Variable from Javascript to AssignMessage inside FormParams

Hi ,

I have set the variables in javascript policy  because i need to pass the below variable dynamically to Assign Message.
Eg:

var val= '<FormParam name ="UID">876ytggjhdgjhgd</FormParam>';
Context.setVariable("FormParamValues",val);



I have tried to pass this FormParamValues in AssignMessage like below:
Eg: 

<Headers content type =" application/ www urlencoded">
<FormParams>{FormParamValues}</FormParams>  

But I am facing error like UID is not passing. Could anyone please suggest how to solve this?

0 5 289
5 REPLIES 5

Hi Dinesh

Yes. my suggestion is to use either JS or AssignMessage to do what you want, and not both. 

For example, in JS you can refer to the variable names to directly set form params. 

 

var kvpairs = {
  "param1" : "value1",
  "param2" : (new Date()).toISOString()
};

for( var p in kvpairs) {
  context.setVariable('request.formparam.' + p, kvpairs[p]);
}

 

This is equivalent to using this assignmessage: 

 

<AssignMessage name='AM-FormParams'>
  <AssignVariable>
    <Name>iso8601FormatString</Name>
    <Value>yyyy-MM-dd'T'HH:mm:ss'Z'</Value>
  </AssignVariable>
  <Set> 
    <FormParams>
      <FormParam name='param1'>value1</FormParam>
      <FormParam name='param2'>{timeFormatUTCMs(iso8601FormatString,system.timestamp)}</FormParam>
    </FormParams>
  </Set> 
</AssignMessage>

 

 If you have dynamic values, you may want to use only JS.  

Thank You Sir. I will try this approach.

How to call API by  pass this Formparam and urlencoded as content type  from javascript policy. Is there any sample coding available? I am new to apigee development. 

I'm not able to understand your question.  Maybe you could ask it a different way.  Or provide a few more words. Or some examples.

I think you want to invoke an http endpoint from the JS policy. 

That is documented, there's an httpClient object for that.  What have you tried?  What are you stuck on? 

Sorry For not understanding my Query.

Here is my AssignMessage 1:

<Add>
<Headers>
<Header name="Content-Type">application/x-www-form-urlencoded</Header>
</Headers>
<Verb>POST</Verb>
<FormParams>
<FormParam name="apiKey">{private.apikey}</FormParam>
<FormParam name="secret">{private.secret}</FormParam>
<FormParam name="userKey">{private.userkey}</FormParam>
<FormParam name="loginID">{loginID}</FormParam>
<FormParam name="password">{Password}</FormParam>
</FormParams>
</Add>
<Set>
<Path>/accounts.login</Path>
</Set>

 

Assign Message 2:

<FormParams>
<FormParam name="apiKey">{private.apikey}</FormParam>
<FormParam name="secret">{private.secret}</FormParam>
<FormParam name="userKey">{private.userkey}</FormParam>
<FormParam name="UID">{UID}</FormParam>
<FormParam name="method">sms</FormParam>
<FormParam name="lang">en</FormParam>
<FormParam name="gigyaAssertion">{GigyaAssertion}</FormParam>
</FormParams>
</Add>
<Set>
<Path>/accounts.tfa.phone.getRegisteredPhoneNumbers</Path>
</Set>

 

AssignMessage 3:

<Add>
<Headers>
<Header name="Content-Type">application/x-www-form-urlencoded</Header>
</Headers>
<verb>POST</verb>
<FormParams>
<FormParam name="apiKey">{private.apikey}</FormParam>
<FormParam name="secret">{private.secret}</FormParam>
<FormParam name="userKey">{private.userkey}</FormParam>
<FormParam name="email">{EmailId}</FormParam>
<FormParam name="password">{Password}</FormParam>
<FormParam name="finalizeRegistration">true</FormParam>
<FormParam name="profile">{ProfileResponse}</FormParam>
<FormParam name="data">{DataResponse}</FormParam>
</FormParams>
</Add>
<Set>
<Path>/accounts.register</Path>
</Set>

 

Like that I had 12 Different API's. Currently I coded with 12 AssignMessages. I need to optimize the Code into 1 AssignMessage and value should pass dynamically based on calling API.

 

I have tried below :

<FormParams>{FormParamValue}</FormParams> in Assign Messages. But it not working.

Note: {FormParamValue} get from Javascript policy .

Kindly Suggest How to pass the value from Javascript to AssignMessage dynamically.

Without Assign Message can we use this approach from Javascript?