Reputation: 121
I am working on a project, where I need to get the events from a hikvision camera. Using the /ISAPI/Event/notification/alertStream endpoint. Which uses multipart/mixed over HTTP server push.
The issue I am having is when I try to io.ReadAll() or io.Copy() and the program just gets stuck. In wireshark I can see the stream of XML events coming in. So I tried scanner see below code. and print out line by line which it does including the --boundary. Which suggest the multipart Reader fails to detect the boundary. Thus the readPartData() never returns.
parser := multipart.NewReader(resp.Body, bod)
for {
log.Println("New Part")
part, err := parser.NextPart()
if err != nil {
log.Println("NextRawPart:", err)
continue
}
da, err := readPartData(part)
if err != nil {
log.Println(err)
continue
}
log.Println(da)
part.Close()
}
func readPartData(part *multipart.Part) (string, error) {
var buffer []byte
scanner := bufio.NewScanner(part)
for scanner.Scan() {
line := scanner.Bytes()
fmt.Println(string(line))
buffer = append(buffer, line...)
buffer = append(buffer, '\n')
}
if err := scanner.Err(); err != nil {
return "", err
}
partData := string(buffer)
return partData, nil
}
Multipart stream
HTTP/1.0 200 OK
MIME-Version: 1.0
Content-Type: multipart/mixed;boundary=boundary
--boundary
Content-Type: application/xml; charset="UTF-8"
Content-Length: 468
<EventNotificationAlert version="1.0" xmlns="http://www.hikvision.com/ver20/XMLSchema">
<protocol>HTTP</protocol>
<channelID>0</channelID>
<dateTime>2023-09-07T10:10:15+10:00</dateTime>
<activePostCount>0</activePostCount>
<eventType>videoloss</eventType>
<eventState>inactive</eventState>
<eventDescription>videoloss alarm</eventDescription>
</EventNotificationAlert>
--boundary
Content-Type: application/xml; charset="UTF-8"
Content-Length: 468
<EventNotificationAlert version="1.0" xmlns="http://www.hikvision.com/ver20/XMLSchema">
<protocol>HTTP</protocol>
<channelID>0</channelID>
<dateTime>2023-09-07T10:10:24+10:00</dateTime>
<activePostCount>0</activePostCount>
<eventType>videoloss</eventType>
<eventState>inactive</eventState>
<eventDescription>videoloss alarm</eventDescription>
</EventNotificationAlert>
--boundary
Content-Type: application/xml; charset="UTF-8"
Content-Length: 468
<EventNotificationAlert version="1.0" xmlns="http://www.hikvision.com/ver20/XMLSchema">
<protocol>HTTP</protocol>
<channelID>0</channelID>
<dateTime>2023-09-07T10:10:34+10:00</dateTime>
<activePostCount>0</activePostCount>
<eventType>videoloss</eventType>
<eventState>inactive</eventState>
<eventDescription>videoloss alarm</eventDescription>
</EventNotificationAlert>
Is there something wrong with the boundary hikvision is sending, or am I doing something wrong? Any suggestions would be greatly appreciated.
Upvotes: 1
Views: 247