'Error while testing Soap Request in python 3.9

I am new in soap protocol. I am using soapUI to make requests on a server and it works fine: enter image description here

Now I would like to run the exact requests on the same server using python requests library. My source code looks like this :

#!/bin/python3

import sys

import requests as req


url = 'http://x.x.x.x:8083/mockServiceSoapBinding'

headers = { "Accept-Encoding": "gzip,deflate",
 "Content-Type": "text/xml;charset=UTF-8",
 "SOAPAction": "http://www.soapui.org/sample/login",
 "Content-Length": "276",
 "Host": "x.x.x.x:8083",
 "Connection": "Keep-Alive",
 "User-Agent": "Apache-HttpClient/4.5.2 (Java/16.0.1)" }


body = """
<soapenv:Envelope xmlns:soapenv="http://schemas.xmlsoap.org/soap/envelope/" xmlns:req="https://httpbin.org/post">
   <soapenv:Header/>
   <soapenv:Body>
   <sam:login>
   <username>Login</username>
   <password>Login123</password>
   </sam:login>
   </soapenv:Body>
</soapenv:Envelope>
"""

res = req.post(url, data = body, headers = headers)
print(res)
print(res.request.headers)

Here is the response :

<Response [403]>
{'User-Agent': 'Apache-HttpClient/4.5.2 (Java/16.0.1)', 'Accept-Encoding': 'gzip,deflate', 'Accept': '*/*', 'Connection': 'Keep-Alive', 'Content-Type': 'text/xml;charset=UTF-8', 'SOAPAction': 'http://www.soapui.org/sample/login', 'Content-Length': '287', 'Host': 'x.x.x.x:8083'}

<soapenv:Envelope xmlns:soapenv="http://schemas.xmlsoap.org/soap/envelope/" xmlns:req="https://httpbin.org/post">
   <soapenv:Header/>
   <soapenv:Body>
   <sam:login>
   <username>Login</username>
   <password>Login123</password>
   </sam:login>
   </soapenv:Body>
</soapenv:Envelope>

I don't know what is wrong with my python script. Can you help ?



Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source