nu123
nu123

Reputation: 139

output of the json not formatted in APIM developer portal

output of the json not formatted in APIM developer portal and HTTP response gives as follows also

HTTP/1.1 200 OK

api-supported-versions: 1.0,2.0
content-length: 1979
content-type: application/json; charset=utf-8
date: Tue, 06 Sep 2022 11:50:38 GMT
strict-transport-security: max-age=15552001; includeSubDomains; preload

in above it say content-type: application/json but out put as below (in the developer portal responses section content type also "application/json" )

{
  "totalRowCount": 1,
  "data": [
    {
      "Code": "",
      "prCode": "CW1208",
      "Name": "CW1208 Quarterly Milestones",
      "description": "",
      "CategoryCode": "Efficiency",
      "dataSource": "",
      "custodian": "",
      "TimeframeCode": "Lag",
      "TypeName": "Single",
      "active": true,
      "InputTypeCode": "Input",
      "rOfficer": "Stephen MCKAY",
      "rOfficerCode": "Stephen ",
      "AggregationMethodCode": "Average",
      "reportingPeriod": "Quarter(s)",
      "responsibleOfficer": "Stephen ",
      "unit": "%",
      "method": "MOREISBETTER",
      "datefrom": "2021-04-01T00:00:00",
      "dateto": "2021-06-30T00:00:00",
      "target": 100.00,
      "actual": 0.00,
      "variance": 0.00,
      "performance": 0.000000,
      "trafficLight": "OFFTRACK",
      "comment": "Quarterly comment.."
    }
  ]
}

Any idea how to fix this?

This is how I added the response enter image description here

in the developer portal example output enter image description here

all operation policies

        <set-header name="Token" exists-action="skip">
            <value>@(context.Request.OriginalUrl.Query.GetValueOrDefault("Token"))</value>
        </set-header>
        <choose>
            <!--QA-->
            <when condition="@(context.Subscription.PrimaryKey.Equals("8ug4ac4a02"))">
                <set-backend-service base-url="https://xxxxx.com/API/api/v1/" />
            </when>
            <!--Other Clients-->
            <otherwise>
                <set-backend-service base-url="https://xxxxemo.xxxx.com/api/v1/" />
            </otherwise>
        </choose>
        <base />
    </inbound>
    <backend>
        <base />
    </backend>
    <outbound>
        <set-header name="X-Powered-By" exists-action="delete" />
        <set-header name="X-AspNet-Version" exists-action="delete" />
        <set-body template="none"></set-body>
        <choose>
            <when condition="@(context.Response.StatusCode == 404)">
                <set-status code="200" reason="No Records Found" />
                <set-body template="none">{"totalRowCount":0,"data":[{"":""}]}</set-body>
            </when>
        </choose>
        <base />
    </outbound>
    <on-error>
        <base />
    </on-error>
</policies> 

Upvotes: 1

Views: 378

Answers (1)

Markus Meyer
Markus Meyer

Reputation: 3967

Please set the response content type to application/json.

Outbound policy:

<outbound>
    <set-header name="X-Powered-By" exists-action="delete" />
    <set-header name="X-AspNet-Version" exists-action="delete" />
    <set-body template="none"></set-body>
    <choose>
        <when condition="@(context.Response.StatusCode == 404)">
            <set-status code="200" reason="No Records Found" />
<!--
            <set-body template="none">{"totalRowCount":0,"data":[{"":""}]}</set-body>
-->
            <set-body>@{
                  var body = new JObject();
                  body["totalRowCount"] = 0;

                  var data = new JArray();
                  var dataValue = new JObject();
                  dataValue[""] = "";
                  data.Add(dataValue);

                  body["data"] = data;
                  return body.ToString();
            }</set-body>
            <set-header name="Content-Type" exists-action="override">
                <value>application/json</value>
            </set-header>
        </when>
    </choose>
    <base />
</outbound>

Result:
enter image description here

Upvotes: 1

Related Questions