Maybe I'm misundertanding, but when it's called the data hasn't arrived yet so available data reports 0? Espruino's event based, so you can't do like on Arduino and just block execution until data arrives. Using the callback as you suggested at the end is the right way to handle it.
Content-Length is sent before everything else, so it's hardly surprising you get that in the header, before you have the 5 bytes.
Espruino is a JavaScript interpreter for low-power Microcontrollers. This site is both a support community for Espruino and a place to share what you are working on.
Maybe I'm misundertanding, but when it's called the data hasn't arrived yet so available data reports 0? Espruino's event based, so you can't do like on Arduino and just block execution until data arrives. Using the callback as you suggested at the end is the right way to handle it.
Content-Length
is sent before everything else, so it's hardly surprising you get that in the header, before you have the 5 bytes.