Skip to content Skip to sidebar Skip to footer

Axios - How To Deal With Big Integers

Here is my request: axios.get(url) .then(res => { console.log(res.data) }) The output is { value: 156144277082605255 } But should be { value: 15614427708260

Solution 1:

My colleague answered the question:

I had to transform my response.data into string. (you may wonder - why the useless function - just to redefine default behavior, which parses string into object with JSON.parse - in here we skip this step)

axios.get(url, { transformResponse: [data  => data] });

and then parse with json-bigint

JSONBigInt.parse(res.data);

Solution 2:

Run this to install json-bigint: npm install json-bigint --save

Then, use the transformResponse as an option of the axios get request

const jsonBig = require('json-bigint');

let url = 'https://your-api';
axios.get(url, { transformResponse: function(response) {
      return jsonBig().parse(response.data);
}});

You can also use this in the following way:

const jsonBig = require('json-bigint');

axios({
  url: 'https://your-api',
  method: 'get',
  transformResponse: function(response) {
    return jsonBig().parse(response);
  },
})

Post a Comment for "Axios - How To Deal With Big Integers"