Nav

Managing Internal Links in API Proxies

If your API returns URLs that provide apps with internal links to other parts of the API, configure the API proxy application to modify the internal links with proxy substitutes, thereby continuing to manage traffic to and from the API through the proxy, where the API can be governed by API Manager.

This document demonstrates how to configure your proxy application to replace links in your API responses without changes to the original API. The features for creating a proxy might not be visible or accessible to you, depending on entitlements you purchased.

Prerequisites

This document assumes:

  • The API has not been built to run on Mule.

  • A proxy application is set up for governing the API through Mule Runtime 3.8.0 or later or API Gateway

  • The API returns responses that contain URLs with internal links to your API. These internal links need to be replaced with proxy versions to maintain the integrity of the API proxy.

  • You downloaded and installed Anypoint Studio

Instructions

The following instructions provide an example configuration in Anypoint Studio that assumes an API response payload having one or more internal links to the API. 

  1. The links that must be changed are in the payload returned by your API. Make this payload easier to scan by transforming it into a string. Add an Object to String Transformer after the outbound endpoint that communicates with your API.

    managing-internal-links-in-api-proxies-9ac7a
  2. After the payload is converted into a string, you can use a Groovy expression to replace the original base URI of the links for the proxy domain. Drag an Expression component into the flow, after the Object to String Transformer.

    managing-internal-links-in-api-proxies-205db

    Configure the Expression component with the following attributes:

    Attribute Value

    Display Name

    Replace BaseUri in payload

    Expression

    message.setPayload(message.getPayload().toString().replace("https://myapi.com/", "http://localhost:8081/"));

When the API returns a link to content in your API’s domain, this expression routes traffic through the proxy. 

If the API output format differs, add a Choice Router to treat each case accordingly:

managing-internal-links-in-api-proxies-ae728

The links that must be changed are in the payload returned by your API.  Make this payload easier to scan by transforming it into a string.

  1. Add an object-to-string-transformer right after the outbound endpoint to your API.

    
           
                   
                
    1
    
    <object-to-string-transformer doc:name="Object to String"/>
  2. Add a Groovy expression to replace the original base URI of the links for the proxy domain, if there is one.  Add an Expression component into the flow, after the Object to String Transformer.


    
            
         
1
2
3
4
5
<expression-component doc:name="Replace BaseUri in payload">
    <![CDATA[
        message.setPayload(message.getPayload().toString().replace("https://myapi.com/","http://localhost:8081/"));
    ]]>
</expression-component>
Attribute Value

Display Name

Replace BaseUri in payload

Expression Child Element

message.setPayload(message.getPayload().toString().replace("https://myapi.com/", "http://localhost:8081/"));

Through this query, whenever the API returns a link to content in your API’s domain, it is changed to be routed through the proxy.

If the API output format differs, add a Choice Router router to treat each case accordingly.

Example


         
      
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
<https:outbound-endpoint exchange-pattern="request-response" method="POST" address="https://myapi.com#[message.inboundProperties['http.request']]" doc:name="Request to API"/>
<choice doc:name="Choice">
            <when expression="">
                <file:file-to-string-transformer doc:name="File to String"/>
            </when>
            <when expression="">
                <scripting:transformer doc:name="Groovy">
                    <scripting:script engine="Groovy"/>
                </scripting:transformer> </when>
            <otherwise>
                <object-to-string-transformer doc:name="Object to String"/>
            </otherwise>
</choice> <expression-component doc:name="Replace BaseUri in payload"> <![CDATA[message.setPayload(message.getPayload().toString().replace("https://myapi.com/", "http://localhost:8083/"));]]>
</expression-component>