gpt4 book ai didi

Azure API 管理查找和替换不适用于 URL

转载 作者:行者123 更新时间:2023-12-03 03:22:36 25 4
gpt4 key购买 nike

我正在运行 API-M,它调用另一个系统 (SAP) 来调用 OData 服务。 OData 服务将返回带有 URL 的响应。我不想公开 API-M 上的内部 URL,但这些策略不起作用。

<?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">

我想屏蔽http://xxxxx.sap-internal.company.cloudhttp://xxxxapim.azure-api.net

基本上,我正在使用这两种策略。我还尝试过单独使用这两种策略之一

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

在跟踪中,我看到了政策。

我不确定为什么它没有正确执行该策略。还有人遇到同样的事情吗?

我尝试使用 API-M 策略进行转换,但无法正常工作。

最佳答案

即使我尝试过,您使用的策略也是正确的。我有另一种方法,使用自定义脚本策略将内部 URL 替换为 OData 服务响应中的屏蔽 URL:

<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>
  • template="liquid" 允许您在 @{} block 内使用脚本。该脚本检索响应正文,使用 Replace 方法将内部 URL 替换为屏蔽 URL,然后返回修改后的响应。

我有一个示例 API 可以在 APIM 中注册,并尝试了您在上面给出的方法来屏蔽 Azure API 管理中某个前服务返回的内部 URL。

enter image description here

我使用函数应用程序进行了重定向,并尝试屏蔽暴露的内部 URL。

#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;
}

输出:

替换 URL 的响应。 enter image description here

关于Azure API 管理查找和替换不适用于 URL,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/76608960/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com