How to properly create a POST request with correct XML to get information back from the ISAPI – Hikvision in Python?

huangapple go评论127阅读模式
英文:

How to properly create a POST request with correct XML to get information back from the ISAPI - Hikvision in Python?

问题

以下是您要翻译的内容:

import requests
from requests.auth import HTTPDigestAuth

headers = {'Content-Type': 'application/xml'}
output = requests.post("http://{}/{}".format(targetIP, apiPath),
         auth=HTTPDigestAuth(login, password), data=payload, headers=headers, timeout=30)

这段代码在不需要payload的所有POST请求中运行良好。我得到了漂亮的XML响应,一切都很好。

但是,一旦我尝试运行此API调用:

ISAPI/ContentMgmt/logSearch

带有以下payload:

"""<?xml version="1.0" encoding="UTF-8"?>
<CMSearchDescription>
	<searchID>CA32CBsadasdasda1A83</searchID>
	<metaId>log.std-cgi.com/Operation/devicePowerOn</metaId>
	<timeSpanList>
		<timeSpan>
			<startTime>2023-02-09T00:00:00Z</startTime>
			<endTime>2023-03-09T23:59:59Z</endTime>
		</timeSpan>
	</timeSpanList>
	<maxResults>100</maxResults>
</CMSearchDescription>"""

我开始收到以下响应:

<?xml version="1.0" encoding="UTF-8" ?>
<ResponseStatus version="1.0" xmlns="http://www.hikvision.com/ver20/XMLSchema">
<requestURL>/ISAPI/ContentMgmt/logSearch</requestURL>
<statusCode>5</statusCode>
<statusString>Invalid XML Format</statusString>
<subStatusCode>badXmlFormat</subStatusCode>
</ResponseStatus>

我承认我对此有些困惑。我直接从浏览器获得了XML负载,XML与使用ISAPI调用的海康威视NVR主页使用的XML完全相同,当然,搜索ID已更改。

我真的不知道这有什么问题,如果有人有任何想法,将不胜感激。

只是提醒一下,我已经尝试过在Google中搜索我的这个特殊问题,并找到了这个问题:https://stackoverflow.com/questions/45995307/hikvision-log-search-rest-api-post-method-giving-invalid-xml-format

不幸的是,这并没有提高我的机会。

英文:

I have tried doing the above with the foloowing:

import requests
from requests.auth import HTTPDigestAuth

headers = {&#39;Content-Type&#39;: &#39;application/xml&#39;}
output = requests.post(&quot;http://{}/{}&quot;.format(targetIP,apiPath),
         auth=HTTPDigestAuth(login, password), data=payload, headers=headers, timeout=30)

This code, works for all post requests where payload is not needed. I am getting beautiful xml responses and everything is dandy.

However, as soon as I tried running this api call:

ISAPI/ContentMgmt/logSearch

with this as payload:

&quot;&quot;&quot;&lt;?xml version=&quot;1.0&quot; encoding=&quot;UTF-8&quot;?&gt;
&lt;CMSearchDescription&gt;
	&lt;searchID&gt;CA32CBsadasdasda1A83&lt;/searchID&gt;
	&lt;metaId&gt;log.std-cgi.com/Operation/devicePowerOn&lt;/metaId&gt;
	&lt;timeSpanList&gt;
		&lt;timeSpan&gt;
			&lt;startTime&gt;2023-02-09T00:00:00Z&lt;/startTime&gt;
			&lt;endTime&gt;2023-03-09T23:59:59Z&lt;/endTime&gt;
		&lt;/timeSpan&gt;
	&lt;/timeSpanList&gt;
	&lt;maxResults&gt;100&lt;/maxResults&gt;
&lt;/CMSearchDescription&gt;&quot;&quot;&quot;

I started getting this in response:

&lt;?xml version=&quot;1.0&quot; encoding=&quot;UTF-8&quot; ?&gt;
&lt;ResponseStatus version=&quot;1.0&quot; xmlns=&quot;http://www.hikvision.com/ver20/XMLSchema&quot;&gt;
&lt;requestURL&gt;/ISAPI/ContentMgmt/logSearch&lt;/requestURL&gt;
&lt;statusCode&gt;5&lt;/statusCode&gt;
&lt;statusString&gt;Invalid XML Format&lt;/statusString&gt;
&lt;subStatusCode&gt;badXmlFormat&lt;/subStatusCode&gt;
&lt;/ResponseStatus&gt;

I admit I am a bit stuck on this. I got the XML payload directly from the browser and the xml is exactly as the one used by the Hikvision's NVR homepage that uses ISAPI calls, with changed searchID of course.

I really dont know what is wrong with this and if anyone has any ideas, they will be greatly appreciated.

Just and FYI, I have already tried googling this particular problem of mine and came accross this question: https://stackoverflow.com/questions/45995307/hikvision-log-search-rest-api-post-method-giving-invalid-xml-format

Unfortunatelly, this hasnt improved my odds.

答案1

得分: 0

您的负载似乎不正确。请将其从<searchID>CA32CBsadasdasda1A83</searchID>更改为以以下格式格式化的任何类型的字符串:<searchID>C92DC285-8F30-0001-40C6-F0EFA8FB18B5</searchID>。如此处所示https://stackoverflow.com/a/62505522

英文:

Your payload does not seem to be correct. Please change it from <searchID>CA32CBsadasdasda1A83</searchID> to any type of string but formatted in this manner: <searchID>C92DC285-8F30-0001-40C6-F0EFA8FB18B5</searchID>. As shown here https://stackoverflow.com/a/62505522

答案2

得分: 0

这实际上需要是一个POST请求,而我当时正在使用GET请求。没有删除问题,因为也许有其他人正在做同样的事情,可能会发现这个答案有帮助。

英文:

Actually, I didnt realise, but this needs to be a POST request, while I was utilising GET request. Not deleting the question, as perhaps someone else is doing the same and might find this answer helpful.

huangapple
  • 本文由 发表于 2023年3月10日 00:26:47
  • 转载请务必保留本文链接:https://go.coder-hub.com/75687416.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定