Problems returning raw in 22.12.0

Just upgraded from 20.12.0 to 22.12.0. Can’t seem to return a raw response like I used to…

Server code:

 fh = open('fftFile', 'rb')
 data = numpy.fromfile(file=fh, dtype=numpy.byte, count=-1, sep='', offset=0)
 return raw(data)

Error is:

[2023-01-17 10:33:37 -0500] [1103836] [ERROR] Exception occurred while handling uri: 'http://sdr-server-101:5055/getSurveyFFTs?fftFile=/opt/data/remote/FREQSWEEP/sweep_2023_01_16_14_33_04_0_6000000000_393216_60_max_0.fft'
Traceback (most recent call last):
  File "/home/slevine/workspace/sanicTest/lib/python3.8/site-packages/sanic/app.py", line 1001, in handle_request
    await response.send(end_stream=True)
  File "/home/slevine/workspace/sanicTest/lib/python3.8/site-packages/sanic/response/types.py", line 156, in send
    await self.stream.send(
  File "/home/slevine/workspace/sanicTest/lib/python3.8/site-packages/sanic/http/http1.py", line 292, in http1_response_header
    if not data and getattr(res, "body", None):
ValueError: The truth value of an array with more than one element is ambiguous. Use a.any() or a.all()

My javascript is simply doing a fetch.

Any help is appreciated!

@app.get("/")
async def handler(request: Request):
    fh = open("fftFile", "rb")
    data = numpy.fromfile(
        file=fh, dtype=numpy.byte, count=-1, sep="", offset=0
    )
    return raw(type(data).__name__)

I will preface this by saying I know little about numpy. But, looks like fromfile returns a ndarray. You need to pass raw a str or bytes.

Looks like you can try this:

@app.get("/")
async def handler(request: Request):
    fh = open("fftFile", "rb")
    data = numpy.fromfile(
        file=fh, dtype=numpy.byte, count=-1, sep="", offset=0
    )
    return raw(data.tobytes())

That second recommendation - data.tobytes() - solved it!!

Thanks for the answer - and the super fast response!

1 Like

The first was not a recommendation, just a quick hack to see what kind of object it was.