Next.js Discord

Discord Forum

ReadableStream in Fetch Request Body Not Parsed Correctly on Server

Unanswered
Mountain Feist posted this in #help-forum
Open in Discord
Mountain FeistOP
Hello! I'm sending a ReadableStream from the client to my server, but on the server, req is received as the Node.js IncomingMessage type instead of the ReadableStream.

When processing the chunks, I only get a single chunk containing [object ReadableStream]. I suspect Next.js is parsing the stream and converting it to a string.


client code:
const response = await fetch("/api/uploadstream", { method: "POST", body: readableStream, headers: { "Content-Type": "application/car", }, });

server code
export const config = { api: { bodyParser: false } }; export default async function handler(req, res) { try { const stream = () => new ReadableStream({ async start(controller) { for await (const chunk of req) { const uint8Array = new Uint8Array(Buffer.from(chunk)); controller.enqueue(uint8Array); } controller.close(); }, }); const uploadResult = await uploadStream({ stream }); res.status(200).json({ message: "Success" }); } catch (error) { res.status(500).json({ error: "Fail" }); } }

How can I ensure the server receives the raw ReadableStream data instead? Thank you! 🙂

0 Replies