I am facing an issue while making a SOAP request from my Python code. I tested the same request using SOAP UI, and it worked successfully. However, when I tried to replicate the request in Python using the Requests library, it didn't work
(I should add that I have already made other requests that worked both in SOAP UI and Python, but none of them involved a bearer token)
first i pass the wsdl is:
import requests
endpoint = "https://externalchannels-pro-
ws.gruposancorseguros.com/Brokers/Document/DocumentService.svc?wsdl"
then i pass the body and bearer token:
auth="""Bearer eyJhbGciOiJSUzI1NiIsIn..."""
body="""<soap:Envelope xmlns:soap="http://www.w3.org/2003/05/soap-envelope" xmlns:tem="http://tempuri.org/" xmlns:san="http://schemas.datacontract.org/2004/07/SancorSeguros.Services.Contract.Document" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance">
<soap:Header>
</soap:Header>
<soap:Body>
<tem:GetCertificateDocument>
<tem:request>
<san:Branch>1500</san:Branch>
<san:CertificatNumber>2</san:CertificatNumber>
<san:Document>ConstanciaPolizaArgentina</san:Document>
<san:OfficialNumber>349947</san:OfficialNumber>
<san:ReferenceNumber>264448</san:ReferenceNumber>
</tem:request>
</tem:GetCertificateDocument>
</soap:Body>
</soap:Envelope>"""
Then i make the request:
headers = {"content-Type": 'application/soap+xml;charset=UTF-8;action="http://tempuri.org/IDocumentService/GetCertificateDocument"',
"Accept-Encoding":"gzip,deflate",
"Accept":"*/*",
"Connection":"Keep-Alive",
"Host":"externalchannels-pro-ws.gruposancorseguros.com",
"Authorization":auth
}
response = requests.request("POST", url=endpoint, data=body, headers=headers)
print(response)
print(response.content)
This returns:
>>> print(response)
<Response [500]>
>>> print(response.content)
b'<s:Envelope xmlns:s="http://www.w3.org/2003/05/soap-envelope"><s:Body><s:Fault><s:Code><s:Value>s:Sender</s:Value></s:Code><s:Reason><s:Text xml:lang="en-US">No es posible finalizar la operaci\xc3\xb3n, favor intentar nuevamente. Activity: 00000000-0000-0000-0000-000000000000</s:Text></s:Reason></s:Fault></s:Body></s:Envelope>'
I used httptoolkit to compare the requests and noticed that SOAP UI adds some lines to the SOAP request. I added the same to my Python code, but it resulted in a different error.
import requests
endpoint = "https://externalchannels-pro-ws.gruposancorseguros.com/Brokers/Document/DocumentService.svc"
auth="""Bearer eyJhbGciOiJSUzI1NiI..."""
body="""<soap:Envelope xmlns:soap="http://www.w3.org/2003/05/soap-envelope" xmlns:tem="http://tempuri.org/" xmlns:san="http://schemas.datacontract.org/2004/07/SancorSeguros.Services.Contract.Document" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance">
<soap:Header xmlns:wsa="http://www.w3.org/2005/08/addressing">
<wsa:Action soap:mustUnderstand="1">
http://tempuri.org/IDocumentService/GetCertificateDocument
</wsa:Action>
<wsa:To soap:mustUnderstand="1">
https://externalchannels-pro-ws.gruposancorseguros.com/Brokers/Document/DocumentService.svc
</wsa:To>
</soap:Header>
<soap:Body>
<tem:GetCertificateDocument>
<tem:request>
<san:Branch>
1500
</san:Branch>
<san:CertificatNumber>
2
</san:CertificatNumber>
<san:Document>
ConstanciaPolizaArgentina
</san:Document>
<san:OfficialNumber>
349947
</san:OfficialNumber>
<san:ReferenceNumber>
264448
</san:ReferenceNumber>
</tem:request>
</tem:GetCertificateDocument>
</soap:Body>
</soap:Envelope>"""
#session = requests.session()
headers = {"content-Type": 'application/soap+xml;charset=UTF-8;action="http://tempuri.org/IDocumentService/GetCertificateDocument"',
"Accept-Encoding":"gzip,deflate",
"Accept":"*/*",
"Connection":"Keep-Alive",
"Host":"externalchannels-pro-ws.gruposancorseguros.com",
"SOAPAction": "",
"Authorization":auth
}
#headers.update({"Content-Length": str(len(body))})
#headers.update({"Authorization": auth})
response = requests.request("POST", url=endpoint, data=body, headers=headers)
print(response)
print(response.content)
the error:
>>> print(response)
>>> print(response)
<Response [500]>
>>> print(response.content)
b'<s:Envelope xmlns:s="http://www.w3.org/2003/05/soap-envelope" xmlns:a="http://www.w3.org/2005/08/addressing"><s:Header><a:Action s:mustUnderstand="1">http://www.w3.org/2005/08/addressing/soap/fault</a:Action></s:Header><s:Body><s:Fault><s:Code><s:Value>s:Sender</s:Value></s:Code><s:Reason><s:Text xml:lang="en-US">No es posible finalizar la operaci\xc3\xb3n, favor intentar nuevamente. Activity: 0c295990-f194-4b68-86ef-50ee24763381</s:Text></s:Reason></s:Fault></s:Body></s:Envelope>'
I compared the requests again and confirmed that the bearer token and the code match. I suspect that SOAP UI may have some mechanism to verify the token correctly, but I haven't been able to figure it out. I would appreciate any assistance as I have been struggling with this issue for several days.
This Are the soapui request working headers:
METHOD:POST
**URL:**https://externalchannels-pro-ws.gruposancorseguros.com/Brokers/Document/DocumentService.svc
HEADERS
accept-encoding: gzip,deflate
authorization: Bearer eyJhbGciOiJSUzI1NiIsInR5cCI6IkpXVCIsImtpZCI6Ik1EWTVPVGc1TVRrek1FUkVRVVF4TVVZNE16WTJPVUkxTmpGQk5FUTRPVE14TnpNelJUVkRRZyJ9.eyJlbWFpbCI6Im1pY2Flb....truncated for security reasons
connection: Keep-Alive
content-length: 1026
content-type: application/soap+xml;charset=UTF-8;action="http://tempuri.org/IDocumentService/GetCertificateDocument"
host: externalchannels-pro-ws.gruposancorseguros.com
user-agent: Apache-HttpClient/4.5.5 (Java/16.0.1)
This are the not working python request headers:
METHOD: POST
URL https://externalchannels-pro-ws.gruposancorseguros.com/Brokers/Document/DocumentService.svc
HEADERS
accept-encoding: gzip,deflate
authorization: Bearer eyJhbGciOiJSUzI1NiIsInR5cCI6IkpXVCIsImtpZCI6Ik1EWTVPVGc1TVRrek1FUkVRVVF...truncated for security reasons
connection: Keep-Alive
content-length: 940
content-type: application/soap+xml;charset=UTF-8;action="http://tempuri.org/IDocumentService/GetCertificateDocument"
host: externalchannels-pro-ws.gruposancorseguros.com
user-agent: python-requests/2.30.0