Be part of JetBrains PHPverse 2026 on June 9 – a free online event bringing PHP devs worldwide together.

VPBram's avatar

Listening for stream in API Request

Hello all

I'm kind of stuck and need some help (samples). I have an endpoint in my Laravel app called "stream", which is reachable through APP_URL/api/stream

Another application is sending JSON files to this endpoint but in a streaming POST request.

I now have to capture the incoming data , but I have no clue where to start with streaming. I've tried to inject Request class ( public function readStream(Request $request) ) and read from that , but it's empty.

ANy pointers/help/samples on how to create a listener for this type of API POST request ?

0 likes
3 replies
nexxai's avatar

I don't have the answer for you, but the OpenAI PHP client obviously does something like this so my first stop would be to check out that package and see how they do it.

VPBram's avatar

Thanks @nexxai , I'll dig into that and see what I can figure out. If I find the solution I'll post here for future reference.

VPBram's avatar

Solved, solution for reading streamed HTTP POST x-ndjson data:

Inject \Illuminate\Http\Request in function parameters (Request $request)

    $content = $request->getContent(true);

    while (!feof($content)) {
        $buffer = fread($content, 1024);
        $receivedJson .= $buffer;
    }

After reaching the end of the stream you will have a $receivedJson which contains a NDJSON string which you can then process further.

Please or to participate in this conversation.