Python发送HTTP SOAP请求获取AXIS相机VAPIX事件列表遇400错误求助
问题描述
我正尝试通过VAPIX API获取AXIS相机的事件列表,用以下Python代码发送HTTP SOAP请求,但收到400响应。我只替换了代码里的IP、用户名和密码。
参考AXIS官方VAPIX文档中关于获取事件列表的内容。
from requests.auth import HTTPDigestAuth import requests url = "http://IP/vapix/services" headers = {"Content-Type" : "application/soap+xml; action=//www.axis.com/vapix/ws/event1/GetEventInstances; Charset=UTF-8"} body = """<SOAP-ENV:Envelope xmlns:SOAP-ENV="//www.w3.org/2003/05/soap-envelope" xmlns:SOAP-ENC="//www.w3.org/2003/05/soap-encoding" xmlns:xsi="//www.w3.org/2001/XMLSchema-instance" xmlns:xsd="//www.w3.org/2001/XMLSchema"><SOAP-ENV:Body><m:GetEventInstances xmlns:m="//www.axis.com/vapix/ws/event1"/></SOAP-ENV:Body></SOAP-ENV:Envelope>""" response = requests.post(url, auth=HTTPDigestAuth('username', 'password'), data = body, headers = headers) print(response)
可能的错误原因及修复方案
1. SOAP命名空间格式错误
你的XML和请求头里的命名空间用了//开头,正确格式应该是http://前缀。比如:
- 请求头的
action值应为http://www.axis.com/vapix/ws/event1/GetEventInstances - XML中所有命名空间(如
xmlns:SOAP-ENV、xmlns:m等)都要补全http://前缀
2. Content-Type头格式不规范
action参数需要用双引号包裹,且Charset应改为小写的charset。正确的Content-Type头写法:
"application/soap+xml; charset=UTF-8; action=\"http://www.axis.com/vapix/ws/event1/GetEventInstances\""
3. 服务路径可能有误
部分AXIS相机的VAPIX服务路径是/axis/services而非/vapix/services,可以尝试修改URL路径。
修复后的示例代码
from requests.auth import HTTPDigestAuth import requests url = "http://IP/axis/services" # 修正服务路径 headers = { "Content-Type": 'application/soap+xml; charset=UTF-8; action="http://www.axis.com/vapix/ws/event1/GetEventInstances"' } body = """<SOAP-ENV:Envelope xmlns:SOAP-ENV="http://www.w3.org/2003/05/soap-envelope" xmlns:SOAP-ENC="http://www.w3.org/2003/05/soap-encoding" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:xsd="http://www.w3.org/2001/XMLSchema"> <SOAP-ENV:Body> <m:GetEventInstances xmlns:m="http://www.axis.com/vapix/ws/event1"/> </SOAP-ENV:Body> </SOAP-ENV:Envelope>""" response = requests.post(url, auth=HTTPDigestAuth('username', 'password'), data=body, headers=headers) print(response.status_code) print(response.text) # 打印响应文本,查看具体错误信息
额外排查建议
- 打印
response.text,AXIS相机通常会在400响应中返回具体错误描述,帮助定位问题 - 确认相机的VAPIX API已启用,且所用账号具备访问事件服务的权限
内容的提问来源于stack exchange,提问作者Grant Eichenberger
相关产品推荐
相关产品推荐

