Reputation: 13
I am testing out a GET
request with APIm using json placeholder api and cannot replace my response body. Any help is appreciated!
JSON response:
{
"userId": 1,
"id": 1,
"title": "delectus aut autem",
"completed": false
}
Here is my policy:
<policies>
<inbound>
<base />
</inbound>
<backend>
<base />
</backend>
<outbound>
<base />
<choose>
<when condition="@(context.Response.StatusCode == 200)">
<set-body>@{
var response = context.Response.Body.As<JObject>();
response["id"] = "Hello World!";
return response.ToString();
}
</set-body>
</when>
</choose>
</outbound>
<on-error>
<base />
</on-error>
</policies>
I end up with this error:
set-body (7.739 ms) { "messages": [ { "message": "Expression evaluation failed.", "expression": "\n var response = context.Response.Body.As();\n response["id"] = 2;\n return response.ToString();\n ", "details": "The message body is not a valid JSON. Unexpected character encountered while parsing value: �. Path '', line 0, position 0.\r\n at Newtonsoft.Json.JsonTextReader.ParseValue()\r\n at Newtonsoft.Json.Linq.JObject.Load(JsonReader reader, JsonLoadSettings settings)\r\n at Microsoft.WindowsAzure.ApiManagement.Proxy.Gateway.MessageBody.AsJObject(Stream stream, Encoding encoding, JsonSerializerSettings settings)\r\n at Microsoft.WindowsAzure.ApiManagement.Proxy.Gateway.MessageBody.As[T](Boolean preserveContent)" }, "Expression evaluation failed. The message body is not a valid JSON. Unexpected character encountered while parsing value: �. Path '', line 0, position 0.\r\n at Newtonsoft.Json.JsonTextReader.ParseValue()\r\n at Newtonsoft.Json.Linq.JObject.Load(JsonReader reader, JsonLoadSettings settings)\r\n at Microsoft.WindowsAzure.ApiManagement.Proxy.Gateway.MessageBody.AsJObject(Stream stream, Encoding encoding, JsonSerializerSettings settings)\r\n at Microsoft.WindowsAzure.ApiManagement.Proxy.Gateway.MessageBody.As[T](Boolean preserveContent)", "Unexpected character encountered while parsing value: �. Path '', line 0, position 0." ] }
Executing this policy and shows that the body is null
. Is there a way to wait for the response body? When I debug it within vscode and step through it, I'm getting the response successfully. Here's my new outbound policy:
<policies>
<inbound>
<base />
</inbound>
<backend>
<base />
</backend>
<outbound>
<base />
<return-response>
<set-header name="Content-Type">
<value>application/json</value>
</set-header>
<set-header name="Accept">
<value>application/json</value>
</set-header>
<set-body>@{
JObject body = null;
try {
body = context.Response.Body.As<JObject>(preserveContent: true);
return "{\"id\":"+ @body["type"] + "}";
} catch (Exception e) {
return JsonConvert.SerializeObject(context.Response.Body);
}
}
</set-body>
</return-response>
</outbound>
<on-error>
<base />
</on-error>
</policies>
Added a trace:
<trace source="test">
<message>@{
return context.Response.Body.As<string>(preserveContent: true);
}</message>
</trace>
test (0.006 ms)
"��\u0002\u0000 ���%\u001d�WV\rm%\n:\u0018�˫�y�ȁ[�P߁��>�\u0000�w9\u0015s\b�èS7���˻\u0013�\u0005늇>qs*��\f�\u0001"
Upvotes: 0
Views: 2804
Reputation: 1
I had the same issue, for me, the header needed to be set in the request. Here's a link https://github.com/MicrosoftDocs/azure-docs/issues/95956
Upvotes: 0
Reputation: 13
Setting the header to Accept-Encoding: *
gave me a successful response. I thought Content-Type: application/json; charset=utf-8
was enough for encoding. I need to do a deeper dive Accept-Encoding but when I saw the error in the trace, it looked like an encoding issue and that's how I arrived with my solution.
Upvotes: 1