Python Forum
Object reference not set to an instance of an object When calling SOAP request - Printable Version

+- Python Forum (https://python-forum.io)
+-- Forum: General (https://python-forum.io/forum-1.html)
+--- Forum: News and Discussions (https://python-forum.io/forum-31.html)
+--- Thread: Object reference not set to an instance of an object When calling SOAP request (/thread-38318.html)



Object reference not set to an instance of an object When calling SOAP request - jaffersaleem - Sep-28-2022

When I call a SOAP request from suds python ,it is giving Object reference not set to an instance of an object.I created the request objects from wsdl and pass the object to the method .It is throwing Object reference not set to an instance of an object..Alternatively I sent the request as Raw XML ,it is also not working .Find the code below .The SOAP request implemented WS Addressing .I updated soap header ((WS Addressing) )and sent it .Before updating soap header(WS Addressing) ,I got Bad Request

def main():

  from suds.sax.element import Element
  from suds.sax.attribute import Attribute
  from suds.sax.text import Raw
  from suds.client import Client
  from suds.bindings import binding
  binding.envns=('SOAP-ENV', 'http://www.w3.org/2003/05/soap-envelope')
  from suds.plugin import MessagePlugin
  class MyPlugin(MessagePlugin):
    def marshalled(self, context):
        foo = context.envelope.getChild('Header')
        foo.attributes.append(Attribute('xmlns:wsa','http://www.w3.org/2005/08/addressing'))
        
  https = suds.transport.https.HttpTransport()
  urlvalue=https://wvua-eucluw-iws-446.aceins.com:446/Xxxxyyy/DocumentService/DocumentService.svc?wsdl
  client = Client(urlvalue ,unwrap=False,transport=https,plugins=[MyPlugin()])

  wsans = ('wsa', 'http://schemas.xmlsoap.org/ws/2004/08/addressing')
mustAttribute = Attribute('SOAP-ENV:mustUnderstand', 'true')
  msgId5 = Element('Action', ns=wsans).setText('http://xxxx.com/Xxxxyyy/DocumentService/2016/06/DocumentService/GetSpecificDocument')

  msgId3 = Element('To', ns=wsans).setText('https://wvua-eucluw-iws-446.aceins.com:446/Xxxxyyy/DocumentService/DocumentService.svc')
  msgId3.append(mustAttribute)
  client.options.cache.clear()
  client.options.headers.clear()
  client.set_options(soapheaders=[ msgId5,msgId3])


  client.set_options(headers={"Content-type" : 'application/soap+xml; charset=UTF-8','SoapAction':'http://xxxx.com/Xxxxyyy/DocumentService/2016/06/DocumentService/GetSpecificDocument'})
  client.options.prettyxml = True

  # Constructing request objects

  spcificdocumentrequest =client.factory.create('ns0:SpecificDocumentRequest')
  spcificdocumentrequest.DocumentGUID ="D589A450-D3CA-4D55-8A0D-1D47E0DD01A2"

  #print (spcificdocumentrequest)
  envrionmentdata=client.factory.create('ns0:EnvironmentData')
  envrionmentdata.MessageGUID = "msg-ID"
  envrionmentdata.CountryCode = "GB"
  envrionmentdata.SourceSystem = "TrackandTrace"
  envrionmentdata.XxxxyyyLogonID = "TrackandTrace"
  envrionmentdata.XxxxyyyEnvironment = "UAT"
  spcificdocumentrequest.EnvironmentData=envrionmentdata

  logging.basicConfig(level=logging.DEBUG)
  logging.getLogger('suds.client').setLevel(logging.DEBUG)



  xml=Raw("""<?xml version="1.0" encoding="UTF-8"?>
<SOAP-ENV:Envelope xmlns:SOAP-ENV=http://www.w3.org/2003/05/soap-envelope xmlns:xsi=http://www.w3.org/2001/XMLSchema-instance xmlns:wsa=http://schemas.xmlsoap.org/ws/2004/08/addressing xmlns:ns0=http://xxxx.com/Xxxxyyy/DocumentService/2016/06 xmlns:ns1=http://www.w3.org/2003/05/soap-envelope xmlns:ns2=http://xxxx.com/Xxxxyyy/DocumentService/2016/06>
   <SOAP-ENV:Header>
      <wsa:Address>http://www.w3.org/2005/08/addressing</wsa:Address>
      <wsa:Action>http://xxxx.com/Xxxxyyy/DocumentService/2016/06/DocumentService/GetSpecificDocument</wsa:Action>
      <wsa:To SOAP-ENV:mustUnderstand="true">https://wvua-eucluw-iws-446.aceins.com:446/Xxxxyyy/DocumentService/DocumentService.svc</wsa:To>
   </SOAP-ENV:Header>
   <ns1:Body>
      <ns0:GetSpecificDocument>
       <ns0:request>
         <ns2:DocumentGUID>D589A450-D3CA-4D55-8A0D-1D47E0DD01A2</ns2:DocumentGUID>
         <ns2:EnvironmentData>
            <ns2:MessageGUID>msg-ID</ns2:MessageGUID>
            <ns2:CountryCode>GB</ns2:CountryCode>
            <ns2:SourceSystem>TrackandTrace</ns2:SourceSystem>
            <ns2:XxxxyyyLogonID>TrackandTrace</ns2:XxxxyyyLogonID>
            <ns2:XxxxyyyEnvironment>UAT</ns2:XxxxyyyEnvironment>
         </ns2:EnvironmentData>
      </ns0:request>
      </ns0:GetSpecificDocument>
   </ns1:Body>
</SOAP-ENV:Envelope>
""")

  # print (spcificdocumentrequest)
  response =client.service.GetSpecificDocument(spcificdocumentrequest)
  
  #response =client.service.GetSpecificDocument(xml)
I got below error
Error:
Message=((Fault){ Code = (Code){ Value = "s:Receiver" Subcode = (Subcode){ Value = "a:InternalServiceFault" } } Reason = (Reason){ Text = "Object reference not set to an instance of an object." } StackTrace = " at xxxx.xxxxyyyy.DocumentService.ServiceImplementation.DocumentService.GetSpecificDocument(GetSpecificDocumentRequest request)



RE: Object reference not set to an instance of an object When calling SOAP request - jaffersaleem - Sep-30-2022

Suds with Soap request as Raw xml is working after i used byte_str to convert the raw xml string to byte_str
please find the code .I used this from suds import byte_str .
But i am getting Object reference not set to an instance of an object when i pass as object instead of xml


import numpy as np # installed with matplotlib
import matplotlib.pyplot as plt
import logging
import suds
import ssl
import requests
import base64

def main():

  from suds.sax.element import Element
  from suds.sax.attribute import Attribute
  from suds import byte_str

  from suds.sax.text import Raw
  from suds.client import Client
  from suds.bindings import binding
  binding.envns=('SOAP-ENV', 'http://www.w3.org/2003/05/soap-envelope')
  from suds.plugin import MessagePlugin
  
  https = suds.transport.https.HttpTransport()
  urlvalue="https://wvua-eucluw-iws-446.aceins.com:446/WorkView/DocumentService/DocumentService.svc?wsdl"

  client = Client(urlvalue ,unwrap=False,transport=https)

  client.options.cache.clear()
  client.options.headers.clear()

  client.set_options(headers={"Content-type" : 'application/soap+xml; charset=UTF-8','SoapAction':'http://xxxx.com/xxxxyyyy/DocumentService/2016/06/DocumentService/GetSpecificDocument'})
  client.options.prettyxml = True
 
  #logging.basicConfig(level=logging.DEBUG)
  #logging.getLogger('suds.client').setLevel(logging.DEBUG)

  inputxml=Raw("""<?xml version="1.0" encoding="UTF-8"?>
<SOAP-ENV:Envelope xmlns:SOAP-ENV="http://www.w3.org/2003/05/soap-envelope" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:wsa="http://schemas.xmlsoap.org/ws/2004/08/addressing" xmlns:ns0="http://xxxx.com/xxxyyyy/DocumentService/2016/06" xmlns:ns1="http://www.w3.org/2003/05/soap-envelope" xmlns:ns2="http://xxxx.com/xxxyyyy/DocumentService/2016/06">
   <SOAP-ENV:Header xmlns:wsa="http://www.w3.org/2005/08/addressing">
      <wsa:Action>http://xxxx.com/xxxyyyy/DocumentService/2016/06/DocumentService/GetSpecificDocument</wsa:Action>
      <wsa:To SOAP-ENV:mustUnderstand="true">https://xxxx.com/xxxyyyy/DocumentService/DocumentService.svc</wsa:To>
   </SOAP-ENV:Header>
   <ns1:Body>
      <ns0:GetSpecificDocument>
       <ns0:request>
         <ns2:DocumentGUID>0D7B80C9-4356-44D4-8C6E-4D479409F314</ns2:DocumentGUID>
         <ns2:EnvironmentData>
            <ns2:MessageGUID>msg-ID</ns2:MessageGUID>
            <ns2:CountryCode>GB</ns2:CountryCode>
            <ns2:SourceSystem>TrackandTrace</ns2:SourceSystem>
            <ns2:xxxxyyyyLogonID>TrackandTrace</ns2:xxxxyyyyLogonID>
            <ns2:xxxxyyyyEnvironment>UAT</ns2:xxxxyyyyEnvironment>
         </ns2:EnvironmentData>
      </ns0:request>
      </ns0:GetSpecificDocument>
   </ns1:Body>
</SOAP-ENV:Envelope>
""")

  message = byte_str(inputxml)

  try:
        print('Start...')
        specificDocumentResponse =client.factory.create('ns0:SpecificDocumentResponse')
        specificDocumentResponse =client.service.GetSpecificDocument(__inject={'msg': message})
    
        for item in specificDocumentResponse:
            extention = item[1].xxxxyyyyDocument.MimeType
            documentdata = item[1].xxxxyyyyDocument.DocumentData
        

        base64_img_bytes = documentdata.encode('utf-8')
        with open('test.'+extention, 'wb') as file_to_save:
             decoded_image_data = base64.decodebytes(base64_img_bytes)
             file_to_save.write(decoded_image_data)

        print ('Passed')
  except RuntimeError as detail:
        print ('Handling run-time error:'+ detail)

  
  print("passed")
main()