David
David

Reputation: 1293

API Management Policy - pass x-www-form-urlencoded body

I have an old asmx web service that can be invoked using postman like so

Postman

I want to expose this via API Management as a JSON endpoint and then have a policy to transform XML but I'm unsure how to set the request details in the policy

I have tried to do this below (and variations of it) but I always get the message error 'requestXML is missing'

<set-body template="liquid">
            <soap:Envelope xmlns:soap="http://www.w3.org/2003/05/soap-envelope" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance">
                <soap:Body>
                    requestXml: "<Request Caller="Harness"><GetEmployerOfferings><EmployerCode>CHCA</EmployerCode></GetEmployerOfferings></Request>"
                </soap:Body>
            </soap:Envelope>
        </set-body>
        <set-header name="Content-Type" exists-action="override">
            <value>application/x-www-form-urlencoded</value>
        </set-header>

How do I pass this to the backend service via an APIM policy?

Upvotes: 2

Views: 3354

Answers (1)

Hury Shen
Hury Shen

Reputation: 15754

For your requirement, I'm a little confused about your json sample as I asked in comment. But I can also provide some information for your reference.

1. If you want to test hard code passing the requestXml to the backend, the correct format should be requestXml=xxxx but not requestXml:xxxx because you use "x-www-form-urlencoded" as content-type (in postman we can use requestXml:xxxx). So the policy in apim should be:

<set-body>requestXml=<Request Caller="Harness"><GetEmployerOfferings><EmployerCode>CHCA</EmployerCode></GetEmployerOfferings></Request></set-body>

But as your body contains xml <>, so it will remove <Request Caller="Harness"><GetEmployerOfferings><EmployerCode>CHCA</EmployerCode></GetEmployerOfferings></Request> automatically after save the policy. Only leave <set-body>requestXml=</set-body>, so test with hard code may not success.

2. And to my understanding, if you want to request the APIM with the json data like below:

{
    "getEmployerOfferings": {
        "requestXml": "<Request Caller=\"Harness\"><GetEmployerOfferings><EmployerCode>CHCA</EmployerCode></GetEmployerOfferings></Request>"
    }
}

If your request json like the sample above, you can refer to the policy below:

<inbound>
    <base />
    <set-body>@{
        var request = context.Request.Body.As<JObject>();
        var xmlstring = request["getEmployerOfferings"]["requestXml"].ToString();
        var result = "requestXml=" + xmlstring;
        return result;
    }</set-body>
    <set-header name="Content-Type" exists-action="override">
        <value>application/x-www-form-urlencoded</value>
    </set-header>
</inbound>

Test the apim, we can find the final request body after operation, it shows: enter image description here

Upvotes: 2

Related Questions