API Management string in payload encoding

Siva Krishna Pokuri 21 Reputation points
2025-01-08T13:38:02.5566667+00:00

I have an API that accepts JSON and transforms to XML for the backend calls using Liquid transformation as sample payload below -

Input Payload from the client

{

"userPriority": "Medium",

"area": "Safety & Security"

}

transformed XML payload to the backend service

<soapenv:Envelope xmlns:soapenv="http://schemas.xmlsoap.org/soap/envelope/">

<soapenv:Header>

    <UserPriority>3-Medium</UserPriority>

    <Area>Safety & Security</Area>

</soapenv:Body>

</soapenv:Envelope>

Because of the '&' unique character, the backend throws an HTTP 500 error message. If the & is encoded, the backend service will accept the request.

I have tried the options below in the Azure APIM transformation logic, but none of them encode special characters using "escape" and "xml_escape."

I appreciate any suggestion to solve the issue.

<set-body template="liquid">

<soapenv:Body>

<UserPriority>{{body.Document.UserPriority}}</UserPriority>

<Area>{{ body.Document.Area | escape }}</Area>

<Area>{{ body.Document.Area | xml_escape }}</Area>

	</soapenv:Body>
	
Azure API Management
Azure API Management
An Azure service that provides a hybrid, multi-cloud management platform for APIs.
2,385 questions
0 comments No comments
{count} votes

2 answers

Sort by: Most helpful
  1. Khadeer Ali 5,055 Reputation points Microsoft External Staff
    2025-01-09T14:21:45.9166667+00:00

    @Siva Krishna Pokuri ,

    Welcome to Microsoft Q&A Platform!

    Thank you for your query.

    To address the issue with special characters causing errors in your backend system, please try to use the replace filter within your Liquid transformation.

    While other options like escape or xml_escape might seem suitable, they might not always handle the full range of characters that need encoding for XML. The replace filter gives you precise control, allowing you to specifically encode characters like &, <, >, ", and ' into their correct XML equivalents: &amp;, &lt;, &gt;, &quot;, and &apos;, respectively.

    Here’s an example of how to do this:

    
    <Area>
    {{
     body.area | replace: '&', '
     }}
    </Area>
    

    Hope this helps. Do let us know if you any further queries.


    If this answers your query, do click Accept Answer and Yes for was this answer helpful. And, if you have any further query do let us know.


  2. Lee, YongKyu 0 Reputation points
    2025-04-30T00:43:28.1733333+00:00

    Hello

    I met a same condition.

    Answer that I found is to use 'Pascal casing for Liquid filter names'.

    https://learn.microsoft.com/en-us/azure/api-management/set-body-policy#supported-liquid-filters

    As example in your issue,

    [before]

    <Area>{{ body.Document.Area | escape }}</Area>

    [after]

    <Area>{{ body.Document.Area | Escape }}</Area>

    0 comments No comments

Your answer

Answers can be marked as Accepted Answers by the question author, which helps users to know the answer solved the author's problem.