Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Configure Axios to utilize these parser and encoder functions within the stringifySafely function #6279

Open
kartik-gupta-ij opened this issue Mar 7, 2024 · 0 comments

Comments

@kartik-gupta-ij
Copy link

kartik-gupta-ij commented Mar 7, 2024

Describe the issue

I have identified that the error is occurring during the serialization process within Axios, specifically in the stringifySafely function. The error message is as follows:

RequesFromCode.js?t=1709818947597:31 TypeError: Do not know how to serialize a BigInt
    at stringify (<anonymous>)
    at stringifySafely (axios.js?v=a7e38bec:812:37)
    at Object.transformRequest (axios.js?v=a7e38bec:854:14)
    at transform (axios.js?v=a7e38bec:1176:15)
    at Object.forEach (axios.js?v=a7e38bec:74:10)
    at Object.transformData (axios.js?v=a7e38bec:1175:17)
    at Axios.dispatchRequest (axios.js?v=a7e38bec:1582:31)
    at Axios._request (axios.js?v=a7e38bec:1872:33)
    at Axios.request (axios.js?v=a7e38bec:1776:25)
    at wrap (axios.js?v=a7e38bec:8:15)
    at Axios.request (axios.js?v=a7e38bec:1780:41)
    at async index.jsx:45:24

Debugging Efforts

As part of my debugging efforts, I have developed custom parser and encoder functions designed to handle BigInts during JSON data parsing. However, I am struggling to integrate these custom functions with Axios.

Custom Functions

Here is a simplified version of the custom functions:

let bigintReviver
let bigintReplacer
if ('rawJSON' in JSON) {
  bigintReviver = function (_key, val, context) {
    if (Number.isInteger(val) && !Number.isSafeInteger(val)) {
      return BigInt(context?.source) 
    }
    return val
  }
  bigintReplacer = function (_key, val) {
    if (typeof val === 'bigint') {
      return JSON.rawJSON?.(String(val))
    }
    return val
  }
}

export const jsonBigIntParse = function (jsonString, reviver, parserConfig) {
  if (bigintReviver) {
    return JSON.parse(jsonString, bigintReviver, parserConfig)
  }
  return JSON.parse(jsonString, reviver)
}

export const jsonBigIntParseStringify = function (value, replacer, space) {
  if (bigintReplacer) {
    return JSON.stringify(value, bigintReplacer, space)
  }
  return JSON.stringify(value, replacer, space)
}

Question

My primary question is: How can I properly configure Axios to utilize these custom parser and encoder functions within the stringifySafely function?

Function link

Additional Information

I believe integrating these custom functions with Axios is crucial in resolving the BigInt serialization issue. Any guidance or suggestions on the best approach to achieve this would be highly appreciated.

Thank you in advance for your assistance.

Example Code

axios({
  method: "POST",
  url: "http://0.0.0.0:6333/"
  data: {
    bigInt: 12223333n,
  },
}).then((response) => {
  console.log(response.data);
});

Expected behavior

status: 200

Axios Version

1.6.7

Adapter Version

No response

Browser

chrome

Browser Version

Latest

Node.js Version

21

@kartik-gupta-ij kartik-gupta-ij changed the title How to send BigInt in json body Configure Axios to utilize these parser and encoder functions within the stringifySafely function Mar 7, 2024
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

1 participant