Python:soapapi不工作,我做错了什么?

2024-09-29 20:27:46 发布

您现在位置:Python中文网/ 问答频道 /正文

我正在尝试使用movildata.com的SOAP API来获取车辆的最后位置,使用IMEI和API密钥,下面是我的代码:

import requests

api_key = 'xxxxxxxxx'
imei = 'xxxxxxxxx'

request = """<?xml version = "1.0" encoding = "utf-8"?>
<soap12: Envelope xmlns: xsi = "http://www.w3.org/2001/XMLSchema-instance" xmlns: xsd = "http://www.w3.org/2001/XMLSchema" xmlns: soap12 = "http://www.w3.org/2003/05/soap-envelope">
  <soap12: Body>
    <getLastLocation xmlns = "http://ws.movildata.com/ws/wsUsers">
      <apikey> {0} </ apikey>
      <IMEI> {1} </ IMEI>
    </ getLastLocation>
  </ soap12: Body>
</ soap12: Envelope>
""".format(api_key, imei)

encoded_request = request.encode('utf-8')

headers = {"Host":"ws.movildata.com",
            "Content-Type": "application/soap+xml; charset=utf-8",
            "Content-Length":str(len(encoded_request))
            }

response = requests.post(url='https://ws.movildata.com/wsUsers.asmx?op=getLastLocation', 
                         headers=headers,
                         data=encoded_request
                         )

print(response.content)
print(response.status_code)

这将返回XML响应和HTTP状态代码500

soap:ReceiverEl servidor no puede procesar la solicitud. ---> Un nombre no puede empezar con el car\xc3\xa1cter \' \', valor hexadecimal 0x20. L\xc3\xadnea 2, posici\xc3\xb3n 9.

我从西班牙语翻译过来,上面写着类似

The server can not process the request. --- & gt; A name can not start with the character ' ';

我确保request中没有任何不必要的空白,但这似乎没有改变任何事情,有没有任何想法我可能做错了什么


Tags: orgcomhttpwsrequestwwwsoaputf
1条回答
网友
1楼 · 发布于 2024-09-29 20:27:46

我看到您的XML格式有一些错误。 试着纠正他们如下所述。如果仍然得到错误,请在下面进行评论

首先,在<soap12:元素中的命名空间声明中,xmlnsxsi之间不能有空格

全部更正:xmlns:xsixmlns:xsdxmlns:soap12

<getLastLocation中,您只有xmlns,没有任何变量名。一些解析器可能不接受它。给出一些伪名称(例如:xmlns:abc

在结束元素中,</和元素名之间有空格

更正以下所有内容:

</getLastLocation>

</soap12:Body>(同时删除:Body之间的空格)

</soap12:Envelope>(同时删除:Envelope之间的空格)

相关问题 更多 >

    热门问题