45

When sending a post request with a Base64 encoded pdf as the body i recieve the error

Error: Request body larger than maxBodyLength limit

I have tried setting both of the following

'maxContentLength': Infinity, 'maxBodyLength': Infinity

in the request config

const result = await axios({
            url: `the url`,
            headers: {'Authorization': `Bearer ${auth_token}`, 'Content-Type': 'application/json'},
            method: 'post',
            data: {
                'ParentId': record_id,
                'Name': file_name,
                'body': body,
                'Description': description ? description : "",
                'maxContentLength': Infinity,
                'maxBodyLength': Infinity
            }
        });

Does anyone have a workaround?

user1781563
  • 685
  • 1
  • 5
  • 11

3 Answers3

72

You are setting

'maxContentLength': Infinity,
'maxBodyLength': Infinity

In your data object. It should be inside the config object, outside the data object.

Aritra Chakraborty
  • 12,123
  • 3
  • 26
  • 35
45

That is what worked for me:

axios({
    method: 'post',
    url: posturl,
    data: formData,
    maxContentLength: Infinity,
    maxBodyLength: Infinity,
    headers: {'Content-Type': 'multipart/form-data;boundary=' + formData.getBoundary()}
})
Oli Folkerd
  • 7,510
  • 1
  • 22
  • 46
Sumith Ekanayake
  • 1,741
  • 17
  • 13
4

If you want to set the maxContentLength and maxBodyLength only one time for all axios calls you can register an interceptor. Add this code before all your axios calls and you body size problems are gone.

axios.interceptors.request.use(request => {
    request.maxContentLength = Infinity;
    request.maxBodyLength = Infinity;
    return request;
})
Schmidko
  • 690
  • 1
  • 7
  • 17