Amy Zhang Thank you for posting this question in Microsoft Q&A. Based on my understanding, you have a backend API which expects content-type as x-www-form-urlencoded
but you need to pass the request body as JSON to APIM. Is that correct?
The above code snippet based on doc: Access and return body as URL-encoded form data is expecting the request body as x-www-form-urlencoded
but the actual request body was JSON and hence it failed. Looking at the Postman request, it formed key/pair value, and you can achieve the same by writing a custom policy with set-body
.
Sample code snippet:
<set-header name="Content-Type" exists-action="override">
<value>application/x-www-form-urlencoded</value>
</set-header>
<set-body>@{
JObject varBody = context.Request.Body.As<JObject>(preserveContent:true);
return $"k1={varBody["k1"]}&k2={varBody["k2"]}&k3={varBody["k3"]}";
}</set-body>
The above code snippet is just for reference, and you can write generic policy expressions based on your need. Refer API Management policy expressions and for more samples, check out Azure API Management Policy Snippets.
I hope this helps with your question and let me know if you have any other. Would be happy to answer any.
If you found the answer to your question helpful, please take a moment to mark it as "Yes" for others to benefit from your experience. Or simply add a comment tagging me and would be happy to answer your questions.