1
votes

Im currently working with policys in Azure API Management and Im interesting in extracting a value that gets returned from the response-body.

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

The response looks like this :

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

I want to get the value thats being returned from the "resourceCode" attribute, in this case "TRA" and then create a new JObject wich I can send to my Azure function.

{
   "resourceCode": "valueFromResponseBody"
}
3

3 Answers

4
votes

Instead of

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

try:

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

That would return Json.NET JObject (http://www.newtonsoft.com/json/help/html/T_Newtonsoft_Json_Linq_JObject.htm) which you later can use to navigate your response.

1
votes
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>
1
votes

Ran into the same issue. The typical JsonConvert.SerializeXmlNode method isn't allowed in policy expressions.

However good ol' JsonConvert.SerializeObject did the trick.

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