Search code examples
javascriptfetch-api

Override JSON Parser in fetch


I am writing a golang program, that sends large int64s, but the integers lose precision due to JS storing them internally as float64s. I found the json-with-bigint library, but I am not sure how to use the library's JSONParser function with fetch().

My code looks like this (it's a method in VueJS):

        fetchList() {
            console.log("fetchList called");
            const url = "/List"
            fetch(url, { 
              credentials: "same-origin",
             })
            .then(response => 
                response.json().then(data => ({
                list: data,
            }))
            .then(res => {
                this.servers = res.servers;
            })
            .catch(err => console.log(err)));
            console.log(this.list);
        },

Any ideas here? I am simply not well versed in JS enough to do this on my own.


Solution

  • Simple: don't use Response.json(). Instead, get the body, then parse it yourself. I.e. instead of

    response.json().then(data => ({
      list: data,
    }))
    

    use

    response.text().then(body => ({
      list: JSONParse(body),
    }))