Pico Wireless reading json response hangs

Firstly thanks for the cool products.

So I am playing with the Pico Wireless Pack using the pimoroni-pico-v0.3.2-micropython-v1.17.uf2

My base idea is to call a REST API and based on the json response do something. I used the pico wireless examples to build my initial code using code from ppwhttp.py

In general the code works, I can read the json and get what I want but I have one main issue. The following code can hang


I can’t seem to recover from it. I used the http_request function as the framework for my code from the example in file ppwhttp.py, so it can be used as reference in the GITHub exmaple - https://github.com/pimoroni/pimoroni-pico/tree/main/micropython/examples/pico_wireless

Secondly, the size returned from the avail_data call seems less than expected. The code as per exampe:

while True:
    if time.ticks_ms() - t_start > timeout:
        print("HTTP request to {}:{} timed out...".format(host_address, port))
        return False

    avail_length = picowireless.avail_data(client_sock)
    if avail_length > 0:

print("Got response: {} bytes".format(avail_length))

And the response is:

Got response: 1369 bytes

But if I use the example code to fetch the data, my response is incomplete. I get the full header but partial body. The code in the example is (which I don’t use) is:

response = b""

while len(response) < avail_length:
    data = picowireless.get_data_buf(client_sock)
    response += data

So I ended up just pulling data until it breaks to get the full response (response body 397 bytes)

response = b""

while True:
        data = picowireless.get_data_buf(client_sock)
        response += data

Now as mentioned above, this works in most cases, and I have the full connections checks in place as per the example in ppwhttp.py but ocasionally, and this may be on the REST side, the picowireless.get_data_buf(client_sock) call just hangs.

My question: Is there any better way to check for hung/timeout on this call? Also if this method is bad, how do I get the correct length from picowireless.avail_data(client_sock) to ensure I read the correct length?