xmlsoapazure-api-managementazure-policyxmlelement

How to set value to XML element with Set-body Policy of API Management


In this case, I want to get an accessTokenTest first, then I will create an SOAP request by using this accessTokenTest. But I have a problem with adding this accessTokenTest to an XML element. Everything fine except filling this value into XML.

<set-variable name="accessTokenTest" value="@(((IResponse)context.Variables["ABCOauth"]).Body.As<JObject>()["access_token"].ToString())" />
<set-body template="liquid">
			<soap:Envelope xmlns:soap="http://schemas.xmlsoap.org/soap/envelope/" xmlns="http://servicxxxx>
				<soap:Body>
					<GetUsersxxx>
						<userIds>
{% for item in body.getUserxxxx.userIds -%}
<string>{{item}}</string>
{% endfor -%}
</userIds>
						<credentials>
							<Client>{{body.getUsersByUserId.credentials.client}}</Client>
							<AccessToken>(string)context.Variables.GetValueOrDefault("accessTokenTest")</AccessToken>
						</credentials>
					</GetUsersxxxx>
				</soap:Body>
			</soap:Envelope>
</set-body>

enter image description here


Solution

  • When using liquid templates in the set-body policy, there is a different syntax for accessing the context object as shown in the docs, similar to what you have used for the client value.

    In your case, the policy would have to look something like this

    <set-variable name="accessTokenTest" value="@(((IResponse)context.Variables["ABCOauth"]).Body.As<JObject>()["access_token"].ToString())" />
    <set-body template="liquid">
      <soap:Envelope xmlns:soap="http://schemas.xmlsoap.org/soap/envelope/" xmlns="http://servicxxxx>
        <soap:Body>
          <GetUsersxxx>
            <userIds>
    {% for item in body.getUserxxxx.userIds -%}
              <string>{{item}}</string>
    {% endfor -%}
            </userIds>
            <credentials>
              <Client>{{body.getUsersByUserId.credentials.client}}</Client>
              <AccessToken>{{context.Variables["accessTokenTest"]}}</AccessToken>
            </credentials>
          </GetUsersxxxx>
        </soap:Body>
      </soap:Envelope>
    </set-body>