Python Forum
Object reference not set to an instance of an object When calling SOAP request
Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
Object reference not set to an instance of an object When calling SOAP request
#1
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)
Larz60+ write Sep-28-2022, 09:16 AM:
Please post all code, output and errors (it it's entirety) between their respective tags. Refer to BBCode help topic on how to post. Use the "Preview Post" button to make sure the code is presented as you expect before hitting the "Post Reply/Thread" button.
Fixed for you this time, please use BBCode tags on future posts.
Reply
#2
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()
Larz60+ write Sep-30-2022, 04:47 PM:
Please post all code, output and errors (it it's entirety) between their respective tags. Refer to BBCode help topic on how to post. Use the "Preview Post" button to make sure the code is presented as you expect before hitting the "Post Reply/Thread" button.

Please heed above. Continuing without using tags may lead to warning.
Fixed for you one more time.
Reply


Possibly Related Threads…
Thread Author Replies Views Last Post
  an object i would like to have Skaperen 3 919 Jun-09-2023, 08:09 AM
Last Post: Iusullin4235
  an object like a dictionary except ... Skaperen 1 1,019 Feb-27-2023, 07:55 PM
Last Post: Gribouillis
  TypeError: 'dict_items' object does not support indexingw Skaperen 3 2,310 May-06-2022, 10:52 PM
Last Post: Skaperen
  creating my own file-like object Skaperen 0 1,170 Feb-06-2022, 08:25 PM
Last Post: Skaperen
  how to destruct a hashlib object? Skaperen 2 1,613 Nov-29-2021, 07:24 PM
Last Post: Skaperen
  clearing a hashlib object Skaperen 0 1,745 Nov-28-2021, 07:54 PM
Last Post: Skaperen
  creating a dummy file object Skaperen 1 1,884 Oct-27-2021, 07:50 AM
Last Post: Gribouillis
  zero referenced object Skaperen 4 2,031 Oct-15-2021, 10:57 PM
Last Post: Skaperen
  object with ever possible attribute Skaperen 3 1,870 Jun-18-2020, 06:21 AM
Last Post: Skaperen
  i want to create my own file-like object Skaperen 0 2,044 Apr-17-2020, 11:47 PM
Last Post: Skaperen

Forum Jump:

User Panel Messages

Announcements
Announcement #1 8/1/2020
Announcement #2 8/2/2020
Announcement #3 8/6/2020