skip to Main Content

I am running API-M that calls another system (SAP) to call an OData Service. The OData service will return a response with URL. I don’t want to expose the internal URL on API-M but the policies are not working.

<?xml version="1.0" encoding="utf-8"?>
<edmx:Edmx Version="1.0" xmlns:edmx="http://schemas.microsoft.com/ado/2007/06/edmx" xmlns:m="http://schemas.microsoft.com/ado/2007/08/dataservices/metadata" xmlns:sap="http://www.sap.com/Protocols/SAPData">
    <edmx:Reference Uri="http://xxxxx.sap-internal.company.cloud/sap/opu/odata/iwfnd/catalogservice;v=2/Vocabularies(TechnicalName='%2FIWBEP%2FVOC_AGGREGATION',Version='0001',SAP__Origin='LOCAL')/$value" xmlns:edmx="http://docs.oasis-open.org/odata/ns/edmx">

I wanted to mask http://xxxxx.sap-internal.company.cloud to http://xxxxapim.azure-api.net

Basically, I am using both policies. I have also tried using one of both policies individually

<outbound>
    <base />
    <find-and-replace from="xxxxx.sap-internal.company.cloud" to="xxxxapim" />
    <redirect-content-urls />
</outbound>

In trace, I am seeing the policies.

I am not sure why is it not performing the policy correctly. Anyone else encountered the same thing?

I tried using API-M Policies to do the conversion but it’s not working correctly.

2

Answers


  1. The policies which you have used is correct even I have tried. I have an another approach use a custom script policy to replace the internal URL with the masked URL in the OData service response:

    <outbound>
        <base />
        <set-body template="liquid">
            @{
                var response = context.Response.Body.As<string>();
                var modifiedResponse = response.Replace("http://xxxxx.sap-internal.company.cloud", "http://xxxxapim.azure-api.net");
                return modifiedResponse;
            }
        </set-body>
    </outbound>
    
    • This template="liquid" allows you to use a script inside the @{} block. The script retrieves the response body, replaces the internal URL with the masked URL using the Replace method, and returns the modified response.

    I had a sample API to get register in APIM and tried that which you had given in the above to mask the internal URL returned by an some ex-service in Azure API Management.

    enter image description here

    I have redirect by using a function app and tried to mask the internal URL exposed.

    #r "Microsoft.AspNetCore.Http"
    
    using System.Net;
    using Microsoft.AspNetCore.Http;
    using Microsoft.Extensions.Logging;
    
    public static async Task<HttpResponseMessage> Run(HttpRequest req, ILogger log)
    {
        // Call the OData service and retrieve the response
        // ...
    
        // Create a new response message
        var response = new HttpResponseMessage(HttpStatusCode.OK);
    
        // Modify the response using policies
        response.Headers.Add("New-Header", "Value");
    
        // Replace the internal URL in the response body with the masked URL
        var responseBody = await response.Content.ReadAsStringAsync();
        responseBody = responseBody.Replace("http://demo.sap-internal.company.cloud", "http://demoapim.azure-api.net");
        response.Content = new StringContent(responseBody, Encoding.UTF8, "application/json");
    
        // Return the modified response
        return response;
    }
    

    Output:

    Response of the Replaced URL.
    enter image description here

    Login or Signup to reply.
  2. I had the same problem with the find-and-replace policy and an SAP OData API. On my end it was caused by an response encoding error. The APIM trace showed "Compression br not supported". When I limited allowed encoding to gzip and deflate only, the expression started to work as expected. Curious if that is your issue too.

    Login or Signup to reply.
Please signup or login to give your own answer.
Back To Top
Search