2

We were working with SOAP APIs in python. We need to dynamically pass the values in a request xml file.

test.xml file:

<Envelope xmlns="http://schemas.xmlsoap.org/soap/envelope/">
    <Body>
        <Add xmlns="http://tempuri.org/">
            <intA>3</intA>
            <intB>4</intB>
        </Add>
    </Body>
</Envelope>

Python script:

from bs4 import BeautifulSoup
import requests
import xml.etree.ElementTree as ET
import lxml
url="http://www.dneonline.com/calculator.asmx?WSDL"
headers = {'content-type': 'text/xml'}
xmlfile = open('test.xml','r')
body = xmlfile.read()


response = requests.post(url,data=body,headers=headers)

print(response.text)

We need to pass intA and intB dynamically from python.

1 Answer 1

5

You can use the format string method. You can specify the positional/keyword arguments in your xml file. While making the requests call, you can pass the values for those arguments.

Here is how your test.xml file should look like:

<Envelope xmlns="http://schemas.xmlsoap.org/soap/envelope/">
    <Body>
        <Add xmlns="http://tempuri.org/">
            <intA>{first_number}</intA>
            <intB>{second_number}</intB>
        </Add>
    </Body>
</Envelope>

and in your Python script, you could load the xmlfile, and while making the posts requests, the arguments can be passed. Here is how:

import requests

url = "http://www.dneonline.com/calculator.asmx?WSDL"
headers = {'content-type': 'text/xml'}
xmlfile = open('test.xml', 'r')
body = xmlfile.read()

response = requests.post(url, data=body.format(first_number=1, second_number=4), headers=headers)

print(response.text)
Sign up to request clarification or add additional context in comments.

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.