How to add SOAPAction to soap request in Python

Viewed 791

I want to make a soap request, however I am not sure how/where to add the SOAPAction. The code I have currently looks as follows:

import requests
url="https://api.nmbrs.nl/soap/v3/EmployeeService.asmx"
headers = {'content-type': 'text/xml'} 

body = """<?xml version="1.0" encoding="utf-8"?>
<soapenv:Envelope xmlns:soapenv="http://schemas.xmlsoap.org/soap/envelope/" xmlns:com="https://api.nmbrs.nl/soap/v3/CompanyService">
   <soapenv:Header>
      <com:AuthHeaderWithDomain>
         <!--Optional:-->
         <com:Username>{}</com:Username>
         <!--Optional:-->
         <com:Token>{}</com:Token>
         <!--Optional:-->
         <com:Domain>{}</com:Domain>
      </com:AuthHeaderWithDomain>
   </soapenv:Header>
   <soapenv:Body>
      <com:List_GetAll/>
   </soapenv:Body>
</soapenv:Envelope>""".format(username, token, domain)
respose = requests.post(url, data=body, headers = headers)

The SOAPACtion I want to add is: SOAPAction = "https://api.nmbrs.nl/soap/v3/CompanyService/List_GetAll"

I tried adding it as a header, or as a parameter of request.post() but that did not work. Could someone please help me with adding the SOAPAction?

2 Answers

According to the SOAP 1.1 specification:

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.       

Thus you should add it into the header like this:

headers = {'content-type': 'text/xml',
           'SOAPAction': 'https://api.nmbrs.nl/soap/v3/CompanyService/List_GetAll'
          } 

Based on the SOAP namespace, you are making a call on SOAP 1.1 for which the SOAPAction is a HTTP header. So you need to send it as a HTTP header:

headers = {
  'content-type': 'text/xml',
  'SOAPAction': 'https://api.nmbrs.nl/soap/v3/CompanyService/List_GetAll'
} 

The problem though I don't think is the SOAPAction header but the fact that you are calling the wrong web service.

Your URL shows https://api.nmbrs.nl/soap/v3/EmployeeService.asmx which does not have a List_GetAll operation in the https://api.nmbrs.nl/soap/v3/CompanyService namespace.

Are you sure you are not looking for the https://api.nmbrs.nl/soap/v3/CompanyService.asmx web service instead?

There you have the operation and the SOAPAction you are trying to use in your call: https://api.nmbrs.nl/soap/v3/CompanyService.asmx?op=List_GetAll

You must match the service URL, with the proper operation name and the proper SOAPAction HTTP header for everything to work.

Related