Next.js Discord

Discord Forum

How to convert ReadableStream<UInt8Array> to stringified/object?

Answered
Griffon Nivernais posted this in #help-forum
Open in Discord
Griffon NivernaisOP
For some reason NextJS is giving me ReadableStreams for the request.body when request is of type Response. How do I extract a string or object from this exactly? I'm struggling and feel like I've tried to both understand the concept and also try as many snippets that I could find, none worked (helpful I know).
Answered by Griffon Nivernais
For fuck sake nevermind it worked, I was just printing request.body instead of the variable.
View full answer

2 Replies

Griffon NivernaisOP
Here's the latest thing I tried, I still ended up with a ReadableStream afterwards:
const extractResultFromStream = async <Type = string,>(
    stream: ReadableStream<Type>,
): Promise<Type> => {
    const chunks = [];

    for await (const chunk of stream) {
        chunks.push(Buffer.from(chunk));
    }

    return Buffer.concat(chunks).toString("utf-8");
};

export async function POST(request: Request) {
    const requestBody = await extractResultFromStream(request.body);

    ...
}
Griffon NivernaisOP
For fuck sake nevermind it worked, I was just printing request.body instead of the variable.
Answer