如何从 API 管理策略中的响应主体获取值?

How to get value from response body in API management policy?

我目前正在处理 Azure API 管理中的策略,我对提取从响应主体返回的值很感兴趣。

<send-one-way-request mode="new">
    <set-url>http://requestb.in/xje199xj</set-url>
    <set-method>POST</set-method>
    <set-header name="Content-Type" exists-action="override">
    <value>application/json</value>
    </set-header>
        <set-body>@{

            //JObject or string?
            string response = context.Response.Body.As<string>(preserveContent: true);
            //something here..
            }
        </set-body>
    </send-one-way-request>

响应如下所示:

"getBookableResourcesResponse": {
    "getBookableResourcesResult": {
      "hasError": false,
      "errorCode": 0,
      "BookableResource": [
        {
          "resourceCode": "TRA",
          "description": "Trailer",
          "group": "F",
          "subGroup": "C",
          "category": "R",
          "dialogType": "CARGO",
          "orgCode": "DECK",
          "length": 14,
          "taraWeight": "7000",
          "grossWeight": "25001",
          "AddResource": [
            {
              "resourceCode": "EXPFIN",
              "description": "Export Finland",
              "dialogType": "UNDEFINED",
              "amount": "0",
              "ticketType": "",
              "orgCode": "EXPFIN",
              "required": "false"
            }.....`

我想获取从“resourceCode”属性返回的值,在本例中为“TRA”,然后创建一个新的 JObject,我可以将其发送到我的 Azure 函数。

{
   "resourceCode": "valueFromResponseBody"
}

而不是

string response = context.Response.Body.As<string>(preserveContent: true);

尝试:

var response = context.Response.Body.As<JObject>(preserveContent: true);

这将 return Json.NET JObject (http://www.newtonsoft.com/json/help/html/T_Newtonsoft_Json_Linq_JObject.htm),您稍后可以使用它来导航您的响应。

My solution:



<send-one-way-request mode="new">
            <set-url></set-url>
            <set-method>POST</set-method>
            <set-header name="Content-Type" exists-action="override">
                <value>application/json</value>
            </set-header>
            <set-body>@{  

                string xml = context.Response.Body.As<string>(preserveContent: true);
                XmlDocument doc = new XmlDocument();
                doc.LoadXml(xml);

            XmlNodeList elemList = doc.DocumentElement.GetElementsByTagName("ResourceCode");           

            string allResources = string.Empty;
            foreach (XmlNode item in elemList)
            {
                if (item.InnerText == "TRA" || item.InnerText == "CONT20" || item.InnerText == "CONT23" || item.InnerText == "CONT26" || item.InnerText == "CONT30" || item.InnerText == "CONT40" || item.InnerText == "CONT45" || item.InnerText == "TRUCK"|| item.InnerText == "VAN" || item.InnerText == "CAMP" || item.InnerText == "CAR")
                {
                   allResources += item.InnerText + ",";

                }                           
            } 

            allResources = allResources.Substring(0, allResources.Length - 1);

            return new JObject(new JProperty("resourceCode", allResources)).ToString();

            }</set-body>
        </send-one-way-request>

运行 进入同样的问题。策略表达式中不允许使用典型的 JsonConvert.SerializeXmlNode 方法。

但是 JsonConvert.SerializeObject 做得很好。

<send-one-way-request mode="new">
    <set-url>http://requestb.in/xje199xj</set-url>
    <set-method>POST</set-method>
    <set-header name="Content-Type" exists-action="override">
    <value>application/json</value>
    </set-header>
        <set-body>@{
            string xml = context.Response.Body.As<string>(preserveContent: true);
            XmlDocument doc = new XmlDocument();
            doc.LoadXml(xml);

            return JsonConvert.SerializeObject(doc);
            }
        </set-body>
</send-one-way-request>