Transform all headers

Huw
Bronze 4
Bronze 4

Is there any way to "loop through all headers" with the AssignMessage policy? It seems not, as you must specify the header name explicitly, but I wondered if there's a better way to achieve what I'm doing.

My goal is to redirect" any backend URLs that show up in the response content or headers, i.e. do a find and replace of "backend URL" with "proxy URL", so I've created a shared flow containing a Javascript policy that does the following:

 

 

/** Dependencies and inputs
 *  Lib.ApimVars.js
 *  Lib.Polyfills.js
 * @property {string} backendUrlRef [optional]
 */

var proxyUrl = context.getVariable('apim.proxy.baseurl');
var backendUrl = context.getVariable(properties.backendUrlRef);
if (!backendUrl){
    backendUrl = context.getVariable('target.url');
}

response.content = polyfills.replaceAll(response.content, backendUrl, proxyUrl);

for (var key in response.headers){
    response.headers[key] = polyfills.replaceAll(response.headers[key], backendUrl, proxyUrl)
};

 

 

I know the response.content could be done with a message template in AssignMessage/Set (as shown below), but haven't found anyway to transform all headers without knowing them in advance.

<Payload>{replaceAll(response.content,target.url,apim.proxy.baseurl)}</Payload>
Solved Solved
1 1 178
1 ACCEPTED SOLUTION

Is there any way to "loop through all headers" with the AssignMessage policy? It seems not, as you must specify the header name explicitly, but I wondered if there's a better way to achieve what I'm doing.

As you suspect, there is no way to loop through headers with the AssignMessage policy. You'd have to explicitly assign to each header.

My goal is to redirect" any backend URLs that show up in the response content or headers, i.e. do a find and replace of "backend URL" with "proxy URL",

Yes, swapping URLs in this way is a somewhat common desire. In the mod_proxy module for Apache HTTP Server, there is a ProxyPassReverse directive that does this nicely (although I don't believe it works on payloads). It worked for payload content as well as the Location header (if any). Apigee doesn't have a built-in equivalent to ProxyPassReverse.

The way I've done this for customers in the past is via JavaScript.

View solution in original post

1 REPLY 1

Is there any way to "loop through all headers" with the AssignMessage policy? It seems not, as you must specify the header name explicitly, but I wondered if there's a better way to achieve what I'm doing.

As you suspect, there is no way to loop through headers with the AssignMessage policy. You'd have to explicitly assign to each header.

My goal is to redirect" any backend URLs that show up in the response content or headers, i.e. do a find and replace of "backend URL" with "proxy URL",

Yes, swapping URLs in this way is a somewhat common desire. In the mod_proxy module for Apache HTTP Server, there is a ProxyPassReverse directive that does this nicely (although I don't believe it works on payloads). It worked for payload content as well as the Location header (if any). Apigee doesn't have a built-in equivalent to ProxyPassReverse.

The way I've done this for customers in the past is via JavaScript.