0

I am trying to parameterize my soap request envelope.
The envelope is written in xml, but it has embedded json for the query data.

ex:

parameter1_value = "2025/06/18 00:00:00-2025/06/18 23:59:59"
parameter2_value = "World Arena"
          json_step = {
                            "QueryData_V1":{
                                "AuthCode":"itsAsecret",
                                "ItemType":"Ticket",
                                "ResultType":"Details",
                                "ResultFormat":"Standard",
                                "Filters":[{
                                    "Field":"CreateDate",
                                    "Operation":"TIME_BETWEEN",
                                    "Value":{parameter1_value}},{
                                    "Field":"center",
                                    "Operation":"EQUALS",
                                    "Value":{parameter2_value}
                                }]
                            }
            }
soap_envelope_mask = f"""<?xml version="1.0" encoding="utf-8"?>
<soapenv:Envelope xmlns:soapenv="http://schemas.xmlsoap.org/soap/envelope/" xmlns:tem='http://tempuri.org/">
                <soapenv:Header></soapenv:Header>
                <soap:Body>
                    <tem:QueryData>
                    <!--Optional:-->
                        <tem:jsonRequest>
                            {json_step}
                        </tem:jsonRequest>
                    </tem:QueryData>
                </soap:Body>
             </soapenv:Envelope>"""

if I try to use soap_envelope_mask.format(**json_step) it gets a KeyError:'"QueryData_V1"' but that is how the envelope is defined. I have also tried many ways to get the json (json.dumps, json.loads, format into the xml, whatever I have try, the json loses it's format or it doesn't indent the JSON portion within the XML and the soap request gets a 500 return.
if I create it without the values instead of parameters it works fine.

If anyone has come across this issue I would appreciate any suggestions

1 Answer 1

0

The 500 error is probably due to not well formed XML or other XML related error. There are a couple of typos in your code as well as a missing namespace for soap prefix.

Assuming the missing prefix is soapenv and removing xml declaration to perform a smoke test on XML with lxml

import json
from lxml import etree

parameter1_value = "2025/06/18 00:00:00-2025/06/18 23:59:59"
parameter2_value = "World Arena"
json_step = {
    "QueryData_V1":{
        "AuthCode":"itsAsecret",
        "ItemType":"Ticket",
        "ResultType":"Details",
        "ResultFormat":"Standard",
        "Filters":[{
            "Field":"CreateDate",
            "Operation":"TIME_BETWEEN",
            "Value":f"{parameter1_value}"},{
            "Field":"center",
            "Operation":"EQUALS",
            "Value":f"{parameter2_value}"
        }]
    }
}

#xml_decl='<?xml version="1.0" encoding="utf-8"?>'
xml_decl=''
soap_envelope_mask = f"""{xml_decl}
<soapenv:Envelope xmlns:soapenv="http://schemas.xmlsoap.org/soap/envelope/" xmlns:tem="http://tempuri.org/">
    <soapenv:Header></soapenv:Header>
    <soapenv:Body>
        <tem:QueryData>
        <!--Optional:-->
            <tem:jsonRequest>
                {json.dumps(json_step, indent=4)}
            </tem:jsonRequest>
        </tem:QueryData>
    </soapenv:Body>
</soapenv:Envelope>"""

print(soap_envelope_mask)

# no error must be thrown. Test also passes with not indented json string.
doc = etree.fromstring(soap_envelope_mask)

RESULT

<soapenv:Envelope xmlns:soapenv="http://schemas.xmlsoap.org/soap/envelope/" xmlns:tem="http://tempuri.org/">
    <soapenv:Header></soapenv:Header>
    <soapenv:Body>
        <tem:QueryData>
        <!--Optional:-->
            <tem:jsonRequest>
                {
    "QueryData_V1": {
        "AuthCode": "itsAsecret",
        "ItemType": "Ticket",
        "ResultType": "Details",
        "ResultFormat": "Standard",
        "Filters": [
            {
                "Field": "CreateDate",
                "Operation": "TIME_BETWEEN",
                "Value": "2025/06/18 00:00:00-2025/06/18 23:59:59"
            },
            {
                "Field": "center",
                "Operation": "EQUALS",
                "Value": "World Arena"
            }
        ]
    }
}
            </tem:jsonRequest>
        </tem:QueryData>
    </soapenv:Body>
</soapenv:Envelope>

Test will fail if json string contains XML reserved characters so it should be enclosed in a CDATA section or html encoded (< becomes &lt; ).

parameter2_value = "World < Arena"

<![CDATA[
                {json.dumps(json_step, indent=4)}
            ]]>
Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.