FastAPI streamingresponse empty when triggered by requests

Viewed 426

I have built a simple API using FastAPI in order to retrieve some information from google cloud storage and return a StreamingResponse as bytes. I do not wish to save the file anywhere because this is for a lightweight front end application.

fast.py content

from fastapi import FastAPI
from fastapi.responses import StreamingResponse
from fastapi.middleware.cors import CORSMiddleware
import uvicorn
import params
from google.cloud import storage

app = FastAPI()
app.add_middleware(
    CORSMiddleware,
    allow_origins=["*"],  # Allows all origins
    allow_credentials=True,
    allow_methods=["*"],  # Allows all methods
    allow_headers=["*"],  # Allows all headers
)


@app.get("/")
def index(filename):
    storage_client = storage.Client()
    bucket = storage_client.bucket(params.BUCKET_NAME)
    blob = bucket.blob(f'data/audio_wav/{filename}')
    bts = blob.download_as_bytes()
    return StreamingResponse(bts, media_type="audio/wav")

if __name__ == "__main__":
    uvicorn.run(app, host="0.0.0.0", port=8000)

test.py content

import requests
url = "http://localhost:8000/?filename=thefile.wav"
response = requests.get(url, stream=True)
print('done')

params.py only contains some globals to define bucket locations so it's not that relevant. I fire up a localhost using python fast.py. Then I run a debug session with a breakpoint in the last line of test.py in VS Code and inspect response.content and it is empty.

Why does this occur?

0 Answers
Related