question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

Axios seems to truncate big data responses intermittently

See original GitHub issue

Describe the bug It appears that axios is intermittently returning async / await requests before they’re completely fulfilled, returning a truncated version of it, in case of payloads with big responses. In my case the response is a json string of ~8M, containing 177.000 lines.

To Reproduce Just a normal async / await request using axios

// http-client.ts
import axios from 'axios';

export class HttpClient {
    private readonly baseUrl: string;

    constructor(baseUrl: string) {
        this.baseUrl = baseUrl;
    }

    public async getBigResponse(): Promise<BigDataStructure> {
        const url = `${this.baseUrl}/test`;
        const { data } = await axios.get(url);

        return data as any;
    }
}

// http-repo.ts
import { HttpClient } from './http-client';

export class HttpRepo {
    private httpClient: HttpClient;

    constructor(httpClient: HttpClient) {
        this.httpClient = httpClient;
    }

    public async getBitData(): Promise<BigDataStructure> {
        let bigData;

        try {
            bigData = await this.httpClient.getBigResponse();
        } catch (error) {
            console.error(error.toString());
            throw error.toString();
        }

        if (bigData.data.length  === undefined) {
            console.log(`Big data undefined: ${bigData}`);
            throw `bigData undefined`;

            try {
                JSON.parse(bigData);
            } catch (e) {
                console.log(`Error parsing bigData into JSON: ${e.toString()}`);
            }

        } else {
            console.log(`big data ok`);
        }

        return bigData;
    }
}

What happens is that sometimes, randomly, the if (bigData.data.length === undefined) is evaluated true as the (partial) json string is returned, and not the whole object I’m expecting. The following console line, if printed, will show a partial json, not enclosed. That has been verified by using the JSON.parse function too.

Expected behavior This should never happen, all the requests should be fulfilled and the object is always returned from the client

Environment:

  • Axios Version: 0.19.0
  • OS: OSX 10.15.1 (Catalina)
  • Additional Library Versions [e.g. React 16.7, React Native 0.58.0] (This refers to my local machine, not to the lambda setup)

Additional context We firstly noticed this problem in an AWS Lambda function, but I was able to reproduce it locally by making multiple requests to the APIs using a simple for loop:

    it('test im not crazy', async () => {
        const httpClient = new HttpClient('https://axios-truncation-issue.free.beeceptor.com');
        const httpRepo = new HttpRepo(httpClient);

        for (let i = 0; i < 50; i++) {
            console.log(`Starting execution of ${i}`);
            const menu = await httpRepo.getBitData();
            expect(menu).toHaveLength(1);
        }
    });

Issue Analytics

  • State:closed
  • Created 4 years ago
  • Reactions:3
  • Comments:14 (1 by maintainers)

github_iconTop GitHub Comments

3reactions
ghostcommented, Mar 5, 2020

@chinesedfan I just want to point out that this also happens when running on nodejs. So there is no XHR, but axios use http adapter instead…

1reaction
MrXplodercommented, Apr 23, 2022

I’m having a request being truncated in my mobile app implementation here using react native… So, i have to switch to “fetch” and at the same time re-implement interceptor for the token and relative api url and everything else that is builded with axios in my app. Seriously that sucks.

It would be nice if we had a solution to the problem.

You should try Gaxios which is a library based on Fetch an has an similar API to Axios. Give it a try

Read more comments on GitHub >

github_iconTop Results From Across the Web

JSON is cut short from HTTP response (Axios) - Stack Overflow
I was using child_process to spawn another process and that one seems to chunk the output. Since the HTTP response can only send...
Read more >
Large response payload gets randomly truncated - Drupal
There appears to be an issue when having the following setting enabled, 'Use httprl to handle drupal_http_request' when dealing with large ...
Read more >
Axios post error: Data truncated for column... - Laracasts
I'm trying to attach a user to an employee after creating the employee. The user already exists. Here is the controller: Copy Code...
Read more >
HTTP requests using Axios - Morioh
As an example, here's how the response looks when requesting data from the GitHub API: axios.get('https://api.github.com/users/mapbox') .then((response) => { ...
Read more >
All Tutorials - Mastering JS
Here's how you can use the `truncate()` function in Lodash to trim strings ... Axios responses have a `data` property that contains the...
Read more >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found