modify soap

In this example we’ll take a look at using hoverpy in conjunction with middleware to modify SOAP data. This example builds upon the previous SOAP example, so I strongly suggest you do that one first.


with HoverPy(modify=True, middleware="python examples/soap/"):

Above, the only real difference with examples/soap/ is that we’re loading up HoverPy with middleware enabled.

    print(client.ResolveIP(ipAddress=ipAddress, licenseKey="0"))

When running this script with python examples/soap/ you should notice your city is ‘New York’. That’s the middleware modifying the result of our SOAP operation.

The XML from ip2geo

Before jumping into the middleware, let’s see what we’ll be modifying.

<?xml version="1.0" encoding="UTF-8"?>
<soap:Envelope xmlns:soap="" xmlns:xsd="" xmlns:xsi="">
      <ResolveIPResponse xmlns="">
            <City>New York</City>
            <Country>United Kingdom</Country>
            <Organization />
            <TimeZone />
            <RegionName />

This is the XML that gets sent back to us after calling the ResolveIP method, as defined in We are interested in modifying the City node.


And here are the important parts of our payload modification script.

from lxml import objectify
from lxml import etree

Above we make sure we are importing the lxml classes that will help us modify the data.

    if "response" in payload_dict and "body" in payload_dict["response"]:
        body = payload_dict["response"]["body"]

Let’s make sure we only operate when we have a response, and it has a body.

            root = objectify.fromstring(str(body))
            ns = "{}"

            ipe = ns + "ResolveIPResponse"
            ipt = ns + "ResolveIPResult"

            root.Body[ipe][ipt].City = "New York"

We parse our xml and turn it into an object. Remember that our program gets our IP address, then tries to geo-locate us based on our IP. The intent of our middleware is to override the city no matter what.


            payload_dict["response"]["body"] = etree.tostring(root)

We finally remove annotations and namespaces that got added to the City element by the objectify library, and serialise the modified body back into the response. And we are done.