0

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:

  1. METHOD:POST

  2. **URL:**https://externalchannels-pro-ws.gruposancorseguros.com/Brokers/Document/DocumentService.svc

    HEADERS

  3. accept-encoding: gzip,deflate

  4. authorization: Bearer eyJhbGciOiJSUzI1NiIsInR5cCI6IkpXVCIsImtpZCI6Ik1EWTVPVGc1TVRrek1FUkVRVVF4TVVZNE16WTJPVUkxTmpGQk5FUTRPVE14TnpNelJUVkRRZyJ9.eyJlbWFpbCI6Im1pY2Flb....truncated for security reasons

  5. connection: Keep-Alive

  6. content-length: 1026

  7. content-type: application/soap+xml;charset=UTF-8;action="http://tempuri.org/IDocumentService/GetCertificateDocument"

  8. host: externalchannels-pro-ws.gruposancorseguros.com

  9. user-agent: Apache-HttpClient/4.5.5 (Java/16.0.1)


This are the not working python request headers:

  1. METHOD: POST

  2. URL https://externalchannels-pro-ws.gruposancorseguros.com/Brokers/Document/DocumentService.svc

    HEADERS

  3. accept-encoding: gzip,deflate

  4. authorization: Bearer eyJhbGciOiJSUzI1NiIsInR5cCI6IkpXVCIsImtpZCI6Ik1EWTVPVGc1TVRrek1FUkVRVVF...truncated for security reasons

  5. connection: Keep-Alive

  6. content-length: 940

  7. content-type: application/soap+xml;charset=UTF-8;action="http://tempuri.org/IDocumentService/GetCertificateDocument"

  8. host: externalchannels-pro-ws.gruposancorseguros.com

  9. user-agent: python-requests/2.30.0

1
  • Hi. Please format your question according to these guidelines. Commented May 21, 2023 at 9:16

1 Answer 1

0

A friend found the problem. It seems that Python was passing the body of the request with spaces between the parameters. After removing them, the request worked.

Before:

 <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>

Working one:

<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> 
Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.