Making a SOAP request using Python requests module

前端 未结 4 1137
滥情空心
滥情空心 2021-02-06 14:48

I used python requests module for REST requests.

I am trying to make a soap request but I wondered couldn’t get an example for this . Here is My soap body and headers.

4条回答
  •  故里飘歌
    2021-02-06 15:17

    I ran into the same problem recently, and unfortunately neither suds nor jurko-suds(a maintained fork of suds) were able to help. This is mainly because suds kept generating wrongly formatted soap envelope (this especially happens if the soap that is supposed to be generated has some content that's supposed to be inside a CDATA) different from what the server was expecting. This was the case even when I tried injecting the soap envelope myself using the __inject option.

    Here's how I solved it using python requests

    import requests
    
    request = u"""
       
             {0}
             {1}
       
       
          
        
            {2}
            ...
        
        
            
                {3}
                ...
            
            
                ...
            
        
        {4}
    ]]>
       
    """.format('Jake_Sully', 
                                  'super_secret_pandora_password',
                                  'TYSGW-Wwhw',
                                  'something_cool',
                                  'SalaryPayment',
                                  'Pandora_title',
                                )
    
    encoded_request = request.encode('utf-8')
    
    headers = {"Host": "http://SOME_URL",
                "Content-Type": "application/soap+xml; charset=UTF-8",
                "Content-Length": str(len(encoded_request)),
                "SOAPAction": "http://SOME_OTHER_URL"}
    
    response = requests.post(url="http://SOME_OTHER_URL",
                         headers = headers,
                         data = encoded_request,
                         verify=False)
    
    print response.content #print response.text
    

    What was really important was to specify the Content-Type in the headers and also the SOAPAction. According to the SOAP 1.1 specifiaction

     The SOAPAction HTTP request header field can be used to indicate the intent of the SOAP HTTP request. The value is a URI identifying the intent. SOAP places no restrictions on the format or specificity of the URI or that it is resolvable. An HTTP client MUST use this header field when issuing a SOAP HTTP Request.       
    

    The value of SOAPAction can usually be found in the wsdl file of the API call that you want to make; if absent from the wsdl file then you can use an empty string as the value of that header

    Also see:

    1. http://archive.oreilly.com/pub/post/unraveling_the_mystery_of_soap.html
    2. http://www.prodigyproductionsllc.com/articles/programming/call-a-webservice-using-soap-and-python/

提交回复
热议问题