fetch-to-curl
fetch-to-curl copied to clipboard
Feature Request: support body from ReadableStream
Problem
fetchToCurl(new Request(myUrl, { method: 'POST', body: someReadableStream })
doesn't work, and yields --data-binary={}
Discussion
It's easy to get into wanting this case when dealing with Request instances from common node tools. For example, I'm using msw
, which... by the time a request is exiting the VM and you're in a proxy handler, body is converted to a stream just from node itself.
Here's a dummy example of what we could do:
async function reqWithStreamToBodyString(req) {
const reader = req.clone().body.getReader();
let result = '';
while (true) {
const { value, done } = await reader.read();
if (done) break;
result += new TextDecoder("utf-8").decode(value);
}
return JSON.stringify(result);
}
Then, rather than a naive if (typeof body === 'object') { ... }
, we could first do a if (typeof req.body?.getReader === 'function) { ... }
and do the needful