1

For a request with a multi-valued queryparam like: https://test.apigee.net?storeIds=abc&storeIds=xyz, how can someone setup an extract variable policy so that there will be a storeIds array like: storeIds=["abc","xyz"]?

Update #1: Using the following in javascript for Apigee:

var arrayOfStoreIds = [];
for (i = 0; i < context.proxyRequest.queryParams['storeIds'].length; i++) { 
  arrayOfStoreIds.push(context.proxyRequest.queryParams['storeIds'][i]);
}

Yields an error:

 `Execution of script failed with error:
    Javascript runtime error:
      "TypeError: Cannot find default value for object ... at line ##"`

The line # referenced points to the 1st line of the for loop

Update #2:

So with this context.proxyRequest.queryParams['storeIds'].length() it works! At least in javascript ... I still don't know how to do this via an extract variable policy...

brandonscript
  • 68,675
  • 32
  • 163
  • 220
pulkitsinghal
  • 3,855
  • 13
  • 45
  • 84

2 Answers2

1

Truth be told, there is no spec on using duplicate query parameters. However, you can clearly see there's an issue when trying to retrieve them; you'd encounter this in php using $_GET, in JavaScript, Node.js, et al.

I'd suggest if you can, do something using pipe or comma separation: storeIds=abc,xyz. Then to extract query parameters using an ExtractVariables policy, you would do something like this:

<?xml version="1.0" encoding="UTF-8" standalone="yes" ?>
<ExtractVariables async="false" continueOnError="true" enabled="true" name="productsVariables">
    <DisplayName>Extract</DisplayName>
    <URIPath>
    <QueryParam name="storeIds">
        <Pattern ignoreCase="true">{storeIdsArray}</Pattern>
    </QueryParam>
    <IgnoreUnresolvedVariables>true</IgnoreUnresolvedVariables>
</ExtractVariables>

In your javascript, you could then do:

try {
    var storeIdsArray = context.getVariable('storeIdsArray').split(',');
    storeIdsArray.forEach(function(storeId) {
        // do something with storeId
    }
}
catch (e) {
    // storeIdsArray wasn't defined or not an array or something else went wrong
}
Community
  • 1
  • 1
brandonscript
  • 68,675
  • 32
  • 163
  • 220
  • I like it! But again javasrcipt gets involved. I want to wait a bit more for someone in apigee to tell me that its just not possible via xml/policy before accepting your answer as the de-facto :) – pulkitsinghal Oct 16 '14 at 19:44
  • I tried it and it didn't work in the policy ;) best I can tell it won't work. – brandonscript Oct 16 '14 at 19:48
1

In raw CGI you can split multiple values with the same queryparam on NULL, but in Apigee, by the time it gets to the Message Processor, the queryparam only contains the first value. So the only way to do this is brute force using the full message.querystring.

If I sent in:

testme?myQueryParam=Y&myQueryParam=X

I have to do a double split using JavaScript and loop through each queryparam doing something like this:

  var text = "";
  var myQueryParams =  context.getVariable('message.querystring');
  var myQueryParamArray = myQueryParams.split(/\&/);
    for (i = 0; i < myQueryParamArray.length; i++) { 
      var myvals = myQueryParamArray[i].split(/\=/);
      text += myvals[1] + ", ";
    }
    context.setVariable('mytext', text);

In this example I create a variable named mytext with the value of "X, Y,"

Michael Bissell
  • 1,210
  • 1
  • 8
  • 14