how do i extract variable from application/x-www-form-urlencoded HTTP POST

Hi

I have an upstream system (sms gateway) that is sending an HTTP POST as content-type of application/x-www-form-urlencoded, but I'm struggling to extract variables from it. I'm not sure how to build out the Extract Variables policy. I've tried a couple, including treating the incoming data as query parameters, but just can't seem to read them. Has anyone any experience/advice?screen-shot-2017-12-21-at-111425.png

I've attached a screen-shot of the trace.

Solved Solved
0 5 5,281
1 ACCEPTED SOLUTION

@kevinanderson, upstream system is sending query parameters as a value of "Request Content" variable. Use the following Java Script snippet to read your query param values and set them as context variables:

// parse query string and set the context variables
function parseQueryString(str) {            
    var pairs = str.split('&');
    for (var p=0; p< pairs.length; p++) {
        var pair = pairs[p].split('=');
context.setVariable(pair[0], pair[1]);
    }
}

// extract the request content
var payload = context.getVariable("request.content");
// strip Request Content= from the payload
payload = payload.substring(payload.indexOf("Request Content=")+17);
// decode the query string value
payload = decodeURIComponent(payload);
// call the function to parse the query string 
parseQueryString(payload);

View solution in original post

5 REPLIES 5

You don’t need a extract variables policy at all, all body parameters will automatically populate variables as request.formparam.{formparamname}, or if your body content format is too complex you can always us JavaScript callouts.


I hope that helps,

Nothing shows up in a trace - other than what I posted in the screen-shot (taken from a trace) - i.e. no variables. I tried using request.formparam.{formparamname} like you suggested, but that was a bust. I've already built apigee proxies that take data from query parameters, xml, json and put it into outbound messages, but how do I go about extracting data from this:

POST /twilio2 HTTP/1.1
Host: xxxx-dev.apigee.net
Content-Type: application/x-www-form-urlencoded


Request+Content=ToCountry%3DGB%26ToState%3DEdinburgh%26SmsMessageSid%3DSMd66b9a81d6ba4674f18110f526f03513%26NumMedia%3D0%26ToCity%3D%26FromZip%3D%26SmsSid%3DSMd66b9a81d6ba4674f18110f526f03513%26FromState%3D%26SmsStatus%3Dreceived%26FromCity%3D%26Body%3Dboo%26FromCountry%3DGB%26To%3D%252B441315102976%26MessagingServiceSid%3DMGef09b549deb253672dad11e32ad2018f%26ToZip%3D%26NumSegments%3D1%26MessageSid%3DSMd66b9a81d6ba4674f18110f526f03513%26AccountSid%3DAC45a09367d8a94af6844930c628ea148c%26From%3D%252B447768418954%26ApiVersion%3D2010-04-01

@kevinanderson are you using a variable prefix? That hides variables from trace... can u post your policy so I can test it?

@kevinanderson, upstream system is sending query parameters as a value of "Request Content" variable. Use the following Java Script snippet to read your query param values and set them as context variables:

// parse query string and set the context variables
function parseQueryString(str) {            
    var pairs = str.split('&');
    for (var p=0; p< pairs.length; p++) {
        var pair = pairs[p].split('=');
context.setVariable(pair[0], pair[1]);
    }
}

// extract the request content
var payload = context.getVariable("request.content");
// strip Request Content= from the payload
payload = payload.substring(payload.indexOf("Request Content=")+17);
// decode the query string value
payload = decodeURIComponent(payload);
// call the function to parse the query string 
parseQueryString(payload);

Spot on! Many thanks.