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

时间:2017-02-27 15:42:05

标签: json azure azure-api-management

我目前正在使用Azure API Management中的policys,我很有兴趣提取从响应体返回的值。

<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" }.....

我想获得从&#34; resourceCode&#34;返回的值。属性,在这种情况下&#34; TRA&#34;然后创建一个新的JObject,我可以发送到我的Azure功能。

{    &#34; resourceCode&#34;:&#34; valueFromResponseBody&#34; }

3 个答案:

答案 0 :(得分:3)

而不是

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

尝试:

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

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

答案 1 :(得分:1)

遇到同样的问题。策略表达式中不允许使用典型的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>

答案 2 :(得分:0)

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>