How to consume JAX WS SOAP Web Service that requires authentication using Python

Introduction

In this tutorial we will see how to consume or call JAX WS based SOAP web service that requires authentication. We 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.

We are 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 we can build or consume the service using different programming languages.

Prerequisites

Authentication example in JAX WS webservice

Python 3.8.1

Call JAX WS SOAP using Postman

First we will see 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.

HTTPS Method: POST

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="http://schemas.xmlsoap.org/soap/envelope/" xmlns:ser="http://service.authentication.ws.jax.roytuts.com/">
   <soapenv:Header/>
   <soapenv:Body>
      <ser:sayHello>
         <arg0>Soumitra</arg0>
      </ser:sayHello>
   </soapenv:Body>
</soapenv:Envelope>

Response Details

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

<?xml version='1.0' encoding='UTF-8'?>
<S:Envelope xmlns:S="http://schemas.xmlsoap.org/soap/envelope/">
    <S:Body>
        <ns2:sayHelloResponse xmlns:ns2="http://service.authentication.ws.jax.roytuts.com/">
            <return>Hello Soumitra</return>
        </ns2:sayHelloResponse>
    </S:Body>
</S:Envelope>

Call JAX WS SOAP using Python

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

We 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', 'http://schemas.xmlsoap.org/soap/envelope/')
		env.setAttribute('xmlns:ser', 'http://service.authentication.ws.jax.roytuts.com/')
		
		#print(self.msg)
		
		#XML input request data
		rawdom = xml.dom.minidom.parseString(self.msg)		
		messagenode = rawdom.firstChild
		
		op = doc.createElement('ser:sayHello')
		op.appendChild(messagenode)
		
		#Header
		header = doc.createElement('soapenv:Header')
		env.appendChild(header)
		
		#Body
		body = doc.createElement('soapenv:Body')
		body.appendChild(op)
		
		env.appendChild(body)
		doc.appendChild(env)
		
		#print(doc.toxml('utf-8'))
		
		return doc.toxml('utf-8')
	
	def send_request(self, url, path, content_type, accept, user, pwd):
		data = self.envelope()
		
		#print(data)
		#print(len(data))
		
		headers = {"Content-type" : content_type, "Accept": accept, "Content-length": len(data), "username": user, "password": pwd}
		conn = http.client.HTTPConnection(url, 8888)

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

In the above Python code, we have imported required packages for calling the SOAP service. We are 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 we 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.

We are using here SOAP version 1.1, hence we are using soapenv as a namespace.

Next we 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.

Request

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

Response

<?xml version=\'1.0\' encoding=\'UTF-8\'?><S:Envelope xmlns:S="http://schemas.xmlsoap.org/soap/envelope/"><S:Body><ns2:sayHelloResponse xmlns:ns2="http://service.authentication.ws.jax.roytuts.com/"><return>Hello Soumitra</return></ns2:sayHelloResponse></S:Body></S:Envelope>

Download

Thanks for reading.

Leave a Comment