How do I append a variable and value in x-www-form-urlencoded form of POST body?

How do I append some value in x-www-form-urlencoded POST body?

Is there a method to access the parts of "context.targetRequest.body.asForm. Can I append a new variable and value into the existing x-www-form-urlencoded payload? I could able to achieve this in JSON format but the project requirement is forcing me to try via x-www-form-urlencoded body.

We are in 4.16.01 Private Edge setup.

Tried the below but did not work:

var body = context.targetRequest.body.asForm;

body.new = "test123";

context.targetRequest.body = body;

Also, is there a way can I set a proxy variable via java script polcies? something like abc.apiKey=""..

Note: All these needed in pre-flow of the traffic.

Solved Solved
1 2 1,531
1 ACCEPTED SOLUTION

There are several ways to do what you want: append a new variable to an existing form payload. Here are two ways I can think of.

  1. Use Javascript. I would try something a little different from what you suggested. Something like this:

    // addFormParam.js
    // ------------------------------------------------------------------
    var content = (context.getVariable('message.content') + '').trim();
    var conjunction = (content === '') ? '' : '&';
    var newFormParam = properties.param_name + '=' + properties.param_value;
    context.setVariable('message.content', content + conjunction + newFormParam);
    	

    And the policy configuration would look like this:

    <Javascript name='JS-AddFormParam' timeLimit='200' >
      <Properties>
        <Property name='param_name'>newparam</Property>
        <Property name='param_value'>1</Property>
      </Properties>
      <ResourceURL>jsc://addFormParam.js</ResourceURL>
    </Javascript>
    
  2. Use AssignMessage. Basically it works the same way, but with less error checking (eg, in case of an empty form.

    <AssignMessage name='AM-AddFormParam'>
      <DisplayName>AM-AddFormParam</DisplayName>
      <IgnoreUnresolvedVariables>false</IgnoreUnresolvedVariables>
      <Set>
        <Payload contentType='application/x-www-form-urlencoded'><![CDATA[{request.content}&newparam=1]]></Payload>
      </Set>
    </AssignMessage>
    	

View solution in original post

2 REPLIES 2

Could able to solve this via below js segment.

var getPayload = context.getVariable("request.content");

var append = "xyz" + api_split[1];

getPayload = getPayload + append;

context.targetRequest.body = getPayload;

There are several ways to do what you want: append a new variable to an existing form payload. Here are two ways I can think of.

  1. Use Javascript. I would try something a little different from what you suggested. Something like this:

    // addFormParam.js
    // ------------------------------------------------------------------
    var content = (context.getVariable('message.content') + '').trim();
    var conjunction = (content === '') ? '' : '&';
    var newFormParam = properties.param_name + '=' + properties.param_value;
    context.setVariable('message.content', content + conjunction + newFormParam);
    	

    And the policy configuration would look like this:

    <Javascript name='JS-AddFormParam' timeLimit='200' >
      <Properties>
        <Property name='param_name'>newparam</Property>
        <Property name='param_value'>1</Property>
      </Properties>
      <ResourceURL>jsc://addFormParam.js</ResourceURL>
    </Javascript>
    
  2. Use AssignMessage. Basically it works the same way, but with less error checking (eg, in case of an empty form.

    <AssignMessage name='AM-AddFormParam'>
      <DisplayName>AM-AddFormParam</DisplayName>
      <IgnoreUnresolvedVariables>false</IgnoreUnresolvedVariables>
      <Set>
        <Payload contentType='application/x-www-form-urlencoded'><![CDATA[{request.content}&newparam=1]]></Payload>
      </Set>
    </AssignMessage>