I have an API which returns a JSON object, one of the attributes of which contains some base64 encoded binary data (a PDF file). I want to use a policy in Azure API Management in front of this API so the response returns just the decoded binary data.
I can decode the binary data into a byte array, but to return it I need to update the response body with the content of that byte array (I've set the content-type header accordingly also). The set-body policy is the one I've tried to use:
<set-body>@{
var response = context.Response.Body.As<JObject>(true);
string content = response.Value<string>("content");
Byte[] bytes = Convert.FromBase64String(content);
return bytes; // Can't do this!
}</set-body>
The above doesn't work, as the return type for set-body has to be a string. I can't convert the binary data to a string as it will get corrupted by ASCII encoding. I can't directly try to assign the value to context.Response.Body as it is read-only within the policy.
Is there any other way of getting Azure API Management to return my byte array in the response?