Turns out the implementation of my code in the OP is correct. The issue, as pointed out by @feras in the comments, was with curl, which uses a buffer. By default, the data is only returned when the buffer is full, or a newline character is encountered. Setting `--no-buffer` solved the issue.