Extract query param in location response header

I am working on the OAuth2.0 Authorising code grant type.

I have received the OAuthorising code in the query param of response location header.It is a 302 redirect .

I am attaching the trace console.

Everything is working fine.

The url in response location header is https://hsbhattacharjee25-eval-test.apigee.net/redirecturl?code=lH1Q8bBm≻ope=.

I want to extract the code out and use it later in the flow.

8767-extract-variable-oauthorising-grant.png

I could not find anything in the doc to extract query param from location response header.

Can anybody explain the code for the same?

Solved Solved
0 3 788
1 ACCEPTED SOLUTION

Maybe the easiest way would be to use a JS callout and just parse the string.

Maybe something like this:

var v = context.getVariable('response.header.location');
var ix = (v == null)? -1 : v.indexOf('?');
if (ix != -1) {
  var query = v.substring(ix + 1);
  var params = query.split('&');
  params.forEach(function(pair) {
    var parts = pair.split('=', 2);
    if (parts[0] == 'code') {
      context.setVariable('savedcode', parts[1]);
    }
  });
}

View solution in original post

3 REPLIES 3

Maybe the easiest way would be to use a JS callout and just parse the string.

Maybe something like this:

var v = context.getVariable('response.header.location');
var ix = (v == null)? -1 : v.indexOf('?');
if (ix != -1) {
  var query = v.substring(ix + 1);
  var params = query.split('&');
  params.forEach(function(pair) {
    var parts = pair.split('=', 2);
    if (parts[0] == 'code') {
      context.setVariable('savedcode', parts[1]);
    }
  });
}

@Dino-at-Google

Is there other easier way to do it like in extract variable policy?

Dino-at-Google please explain the code a little bit.