Previously, I successfully piped the response of another API call to a Next.js API response like this:
export default async function (req, res) {
// prevent same site/ obfuscate original API
// some logic here
fetch(req.body.url).then(r => {
r.body.pipe(res);
}).catch(err => {
console.log(err);
res.status(500).send("Invalid Url");
})
}
It worked flawlessly. However, now the response.body
returned by the fetch API no longer has the pipe
method. Instead, it offers pipeTo
and pipeThrough
methods. Unfortunately, the Next.js res:NextApiResponse
cannot be assigned to a WritableStream
.
I attempted creating a blob
(await r.blob()
) and using res.send(blob)
and res.send(blob.strem())
. Initially, it seemed to work but the data received by the frontend turned out to be incorrect (Essentially,
fetch().then((res) => res.blob()).then((blob) => URL.createObjectURL(blob)))
resulted in corrupted data).