【问题标题】:How to add SOAPAction to soap request in Python如何在 Python 中将 SOAPAction 添加到肥皂请求中
【发布时间】:2021-09-14 09:05:42
【问题描述】:

我想发出一个肥皂请求,但是我不确定如何/在哪里添加 SOAPAction。我目前的代码如下:

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)

我要添加的 SOAPACtion 是: SOAPAction = "https://api.nmbrs.nl/soap/v3/CompanyService/List_GetAll"

我尝试将其添加为标头,或作为 request.post() 的参数,但这不起作用。 有人可以帮我添加 SOAPAction 吗?

【问题讨论】:

    标签: python soap python-requests


    【解决方案1】:

    根据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.       
    

    因此你应该像这样将它添加到标题中:

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

    【讨论】:

    • 我尝试过这种方式,但是这会导致以下错误:Server did not recognize the value of HTTP Header SOAPAction: https://api.nmbrs.nl/soap/v3/CompanyService/List_GetAll.
    • 错误状态为标头的“值”,因此它确实正确读取了标头。因此,您的问题在于 URL - 它可能是错误的。你能再检查一遍吗? @WibeMan
    【解决方案2】:

    基于 SOAP 命名空间,您正在调用 SOAP 1.1,其 SOAPAction 是 HTTP 标头。所以你需要将它作为 HTTP 头发送:

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

    虽然我认为问题不是 SOAPAction 标头,而是您调用了错误的 Web 服务这一事实。

    您的网址显示https://api.nmbrs.nl/soap/v3/EmployeeService.asmx,它在https://api.nmbrs.nl/soap/v3/CompanyService 命名空间中没有List_GetAll 操作。

    您确定您不是在寻找 https://api.nmbrs.nl/soap/v3/CompanyService.asmx 网络服务吗?

    这里有您尝试在呼叫中使用的操作和 SOAPAction:https://api.nmbrs.nl/soap/v3/CompanyService.asmx?op=List_GetAll

    您必须匹配服务 URL、正确的操作名称和正确的 SOAPAction HTTP 标头才能正常工作。

    【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2011-02-10
    • 1970-01-01
    • 1970-01-01
    • 2019-03-22
    相关资源
    最近更新 更多