Consume JAX WS SOAP Web Service That Requires Authentication Using Python

Auth SOAP Service

In this tutorial I will see how to consume or call JAX WS based SOAP web service that requires authentication. I will use Python program to consume the SOAP service. In my other example I had shown how to call SOAP webservice using Python but that service did not require any authentication.

I am going to use my existing JAX WS based SOAP service which I previously created using Java programming language. As the web service is language agnostic so you can build or consume the service using different programming languages.


Authentication example in JAX WS webservice

Python 3.8.1/3.9.1/3.11.5

Call JAX WS SOAP using Postman

First I will show you how to call the SOAP webservice using Postman tool. You can also use SOAP UI for SOAP service testing but I think Postman is light-weight tool.

The WSDl URL is http://localhost:8888/jax-ws-auth/hello?WSDL.

Request Details

Here are the request details of the temperature converter SOAP service.


Service URL: http://localhost:8888/jax-ws-auth/hello

Content-Type: text/xml (this should go in the Headers)

username: user (this should go in the Headers)

password: pass (this should go in the Headers)

Request Body:

<soapenv:Envelope xmlns:soapenv="" xmlns:ser="">

Response Details

Clicking on the Send button will give you the following response:

<?xml version='1.0' encoding='UTF-8'?>
<S:Envelope xmlns:S="">
        <ns2:sayHelloResponse xmlns:ns2="">
            <return>Hello Soumitra</return>

Call JAX WS SOAP using Python

Now I will see how to call JAX WS SOAP service using Python programming language.

I will write the Python code in Object Oriented way.

import http.client
import xml.dom.minidom

class soap_consumer:

	def __init__(self, msg):
		self.msg = msg
	def envelope(self):
		doc = xml.dom.minidom.Document()
		env = doc.createElement('soapenv:Envelope')
		env.setAttribute('xmlns:soapenv', '')
		env.setAttribute('xmlns:ser', '')
		#XML input request data
		rawdom = xml.dom.minidom.parseString(self.msg)		
		messagenode = rawdom.firstChild
		op = doc.createElement('ser:sayHello')
		header = doc.createElement('soapenv:Header')
		body = doc.createElement('soapenv:Body')
		return doc.toxml('utf-8')
	def send_request(self, url, path, content_type, accept, user, pwd):
		data = self.envelope()
		headers = {"Content-type" : content_type, "Accept": accept, "Content-length": len(data), "username": user, "password": pwd}
		conn = http.client.HTTPConnection(url, 8888)

		conn.request("POST", path, data, headers)
		response = conn.getresponse()
		resp_data =
		if response.status == 200:
			return resp_data
			return 'Failed:' + str(response.status) + str(resp_data)

In the above Python code, I have imported required packages for calling the SOAP service. I am going to use XML as request in the body parameter of the service. The msg variable will hold the input request passed as XML.

You can always modify the code as per your requirements and idea here is to show you how to call SOAP service using Python programming.

Next I define a function which will build the envelop if it is XML.

The envelop which I have created here will work just for the specified service which was earlier tested using Postman tool. You have to modify the code if you have different structure.

I am using here SOAP version 1.1, hence I am using soapenv as a namespace.

Next I define send_request() function which takes a number of parameters for SOAP service.

Testing the Program

Executing the above Python program with below code snippets will give you the expected output.


swsc = soap_consumer('<arg0>Soumitra</arg0>')
resp = swsc.send_request('localhost', '/jax-ws-auth/hello', 'text/xml; charset=utf-8', 'text/xml', 'user', 'pass')


<?xml version=\'1.0\' encoding=\'UTF-8\'?><S:Envelope xmlns:S=""><S:Body><ns2:sayHelloResponse xmlns:ns2=""><return>Hello Soumitra</return></ns2:sayHelloResponse></S:Body></S:Envelope>

That’s all about how to consume SOAP web service that requires authentication.

Source Code


Leave a Reply

Your email address will not be published. Required fields are marked *