1

Trying FastAPI's StreamingResponse, however getting TypeError: stream.on is not a function in the browser:

    const response = await axios.get("http://localhost:8000/stream", {
      responseType: "stream",
    });

    const stream = response.data;

    stream.on("data", (data) => {
      console.log(event.data);
    });

Server endpoint (code snippet):

    def get_stream():
        yield 'a'
        yield 'b'
        yield 'c'


    @app.get("/stream")
    async def main():
        return StreamingResponse(get_stream(), media_type="text/plain")

I also tried passing --h11-max-incomplete-event-size 0 argument to uvicorn command as mentioned here: https://www.uvicorn.org/settings/

Chris
  • 18,724
  • 6
  • 46
  • 80
anthnyprschka
  • 301
  • 3
  • 14
  • 1
    Have you seen https://stackoverflow.com/questions/60048180/is-it-possible-to-post-a-responsetype-stream-in-axios ? It doesn't seem like `stream` is valid in a browser context in axios. In any case, the response type wouldn't differ from a regular response, just that FastAPI would hand over data piecemeal. – MatsLindh Mar 29 '23 at 11:50
  • Does this answer your question? [FastAPI StreamingResponse not streaming with generator function](https://stackoverflow.com/questions/75740652/fastapi-streamingresponse-not-streaming-with-generator-function) – Chris Mar 29 '23 at 11:51
  • @Chris no because I'm streaming specifically to the browser. As @MatsLindh pointed out it's an issue with axios, I got it working using `fetch` – anthnyprschka May 13 '23 at 22:55

0 Answers0