How to assign the value of a required header of the request onto the target endpoints. The header would contain the host information.

Not applicable

How to assign the value of a required header of the request onto the target endpoints. The header would contain the host information.

The uri has to be appended to the host from the header based on the resources.

Please help with a solution and also suggest the optimized method

,

How to assign the value of a required header of the request onto the target endpoints. The header would contain the host information.The remaining path has to be appended as per the resources.

Please suggest the most optimized method and also the solution.

1.> Assign Message

2.> Java Script

0 2 620
2 REPLIES 2

Not applicable

For Example:

Let the host name be: https://gst.ind.com (received from the value of one header in request)

2 resources:

/upload

/update

The target endpoint would look like

https://gst.ind.com/upload

https://gst.ind.com/update

I understand that you would like to use a JavaScript policy or an AssignMessage policy to set the target url. You said that you would like to use the value obtained from a header in the request.

This is possible.

There is a specific variable that you may set, in the Target Request flow, that specifies the target to use: target.url

The AssignMessage policy, specifically the AssignVariable element, can be used to set the target.url variable. But, AssignMessage/AssignVariable approach allows you to set the target.url to a fixed value, or to the value contained within another variable. This approach does not allow you to compose the target URL by parts, such as scheme, hostname, and path.

To do that you must use the JavaScript policy.

Within the JS code, I suggest that you take advantage of the uri.js module available here.

Here is some example code that might work for you:

// setTargetUrl.js
// ------------------------------------------------------------------
//
// Dynamically assign the target.url. 
//
// 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());
outboundUri.hostname(context.getVariable('request.header.targethost');
context.setVariable('target.url', outboundUri.toString());

What that does:

  • gets the path and query from the inbound request (like /upload?foo=bar)
  • appends that to the basepath in the target
  • specifies a dynamic hostname for the target
  • sets it into the target.url variable

i haven't tried it, but it looks pretty close to correct. This JS policy must run in the Target Request context.

There is an outstanding change request for the AssignMessage policy that will allow this. (APIRT-1180 - AssignVariable should allow a message template)