got/source/as-promise/index.ts
Line 144 in ecb0534
I'm trying to configure Got to retry on my POST requests. I pass Node's ReadStream created by fs.createReadStream()
to the body
option. On retries, I'm getting Cannot retry with consumed body stream
error. When trying to manipulate the body property on beforeRetry
or afterResponse
hooks I'm getting Cannot assign to read only property 'body'
.
I'm afraid there is no way to reassign a new stream to the body on retries and the mentioned at the top of this issue line doesn't let you retry even if the stream position is 0.
My simplified code looks like this:
const gotChunkInstance = this.gotInstance.extend({
retry: {
limit: retryLimit,
methods: ['GET', 'POST', 'PUT', 'PATCH', 'HEAD', 'DELETE', 'OPTIONS', 'TRACE']
},
hooks: {
// beforeRetry: [
// async (error, retryCount) => {
// // This doesn't work. Throws "Cannot assign to read only property"
// gotChunkInstance.defaults.options.body = await chunkStreamProvider.getChunkStream();
// }
// ],
afterResponse: [
async (response, retryWithMergedOptions) => {
if (this.shouldRetry(response)) {
const updatedOptions: Partial<Options> = {
body: await chunkStreamProvider.getChunkStream() // if this assignment is missing retryWithMergedOptions throws "Cannot retry with consumed body stream"
};
gotChunkInstance.defaults.options.merge(updatedOptions);
return retryWithMergedOptions(updatedOptions); // This is not working - throws "Cannot assign to read only property"
}
else
return response;
}
]
}
});
const ongoingRequestPromise = gotChunkInstance.post(uploadUrl, {
body: await chunkStreamProvider.getChunkStream()
});
await ongoingRequestPromise;
Pay now to fund the work behind this issue.
Get updates on progress being made.
Maintainer is rewarded once the issue is completed.
You're funding impactful open source efforts
You want to contribute to this effort
You want to get funding like this too