axios icon indicating copy to clipboard operation
axios copied to clipboard

Reading FormData for the second time will timeout

Open DokiDoki1103 opened this issue 1 year ago • 2 comments

Describe the bug

If the first request of using form data fails, I need to retry, but the second retry will definitely get stuck and no new request will be sent. It is suspected that the stream has been read and cannot be reused

After trying again, it will get stuck and error timeouts will occur one after another

import axiosRetry from "axios-retry";
import axios from "axios";
import FormData from 'form-data'

const form = new FormData()
form.append('my_field', 'my_value');


const instance = axios.create({
    headers: form.getHeaders(),
    maxContentLength: Infinity,
});


axiosRetry(instance, {
    retries: 3, 
    retryCondition(error) {
        console.log("retrying")
        return true
    }
});


instance.post('http://example.com/submit-form', form)
    .then((response) => {
        console.log('Success:', response.data);
    })
    .catch((error) => {
        console.error('Error:', error);
    });

To Reproduce

No response

Code snippet

No response

Expected behavior

No response

Axios Version

No response

Adapter Version

No response

Browser

No response

Browser Version

No response

Node.js Version

No response

OS

No response

Additional Library Versions

No response

Additional context/Screenshots

No response

DokiDoki1103 avatar Jun 19 '24 08:06 DokiDoki1103

Can confim, im having this problem as well and it really seems that it stems from the consumed stream being, well, already consumed when the request is retried

manelcastro avatar Jul 25 '24 14:07 manelcastro

You can't re-read the stream payload. Use the native FormData & Blob classes or their spec-compliant polyfill like formdata-node (for node older than v18).

DigitalBrainJS avatar Aug 01 '24 12:08 DigitalBrainJS