Docker stats API returns partial JSON

Server Version: 1.11.1
Kernel Version: 3.10.0-327.13.1.el7.x86_64
Operating System: Red Hat Enterprise Linux Server 7.2 (Maipo)
OSType: linux
Architecture: x86_64

I have a Perl script that send stats request to socket /var/run/docker.sock: GET /containers/$id/stats?stream=0 HTTP/1.1

From time to time, I get bad response that starts with:

It doesn’t even have the proper HTTP response header