Using a variable in the target url

Currently our backend separates customer accounts into different url paths in the general form: http://example.com/foo/{accountName}/api/login

I'd like to tie this together in Apigee, and route to the appropriate account's endpoint based on, for instance, a claim in a JWT. However, I can't find a good generalized way to go about this that doesn't involve adding multiple policies at different points on each endpoint.

Any suggestions?

0 4 1,978
4 REPLIES 4

I don't know why you would need to attach policies at "multiple points".

There is a way to assign a dynamic target url in Apigee Edge. Usually it's done with a single JavaScript or AssignMessage policy attached in the request flow of the Target Endpoint. You may want to search this forum for prior answers that describe how to do this and give code samples.

If these prior answers don't satisfy, please explain what is special about your situation, and we'll try to help.

So, it looks like what I want is <Set><Path> in an assign message policy, but the documentation says that's not currently working. I have a Shared Flow where I'm attempting to get the information I need to rebuild the target url, but target.host is coming back empty (I'm in the target pre-flow / request at this point)

I have used JavaScript, with a URL-parsing library. like this:

// setTargetUrl.js
// ------------------------------------------------------------------
//
// Forcibly set the target.url. Necessary when a proxy invoked via a
// LocalTargetConnection does not set the proxy.pathsuffix variable.
//
// This code relies on https://medialize.github.io/URI.js to parse the URI.
// You must:
//   - download the .js file and place it in the resources/jsc directory
//   - reference the .js file in the JS policy configuration:
//
//       <IncludeURL>jsc://URI.js</IncludeURL>
//
// created: Wed Jun 14 14:35:59 2017
// last saved: <2017-August-09 14:41:54>


var inboundUri = new URI(context.getVariable('proxy.url'));
var inboundPath = inboundUri.path();
var outboundUri = new URI(context.getVariable('target.url'));
var originalPath = outboundUri.segment();
var pathToAppend = inboundUri.segment();
outboundUri.segmentCoded(originalPath.concat(pathToAppend));
outboundUri.query(inboundUri.query());
context.setVariable('target.url', outboundUri.toString());



Not applicable

Hi @Zach Tibbitts, You can use below type of code in your target endpoint and set the variable "xyz" somewhere in the preflow using javascript or assignmessage policy according to your logic.

HTTPTargetConnection>

<LoadBalancer> <Server name="ABC"/> </LoadBalancer>

<Path>/hi/{xyz}/mnop</Path>

</HTTPTargetConnection>