Home > Software engineering >  Could not parse response body as JSON
Could not parse response body as JSON

Time:11-27

Hi today i came beceaue i have an error of encoding with your configs with two example one who works with no error one who are glitch. With my code of course. Thanks for your help <3

The library axios-get-json-respone allows to have a configuration and to parse the json more easily

example when i type doge response of the api : '\x03\x0E�{"dogecoin":{"usd":0.092733}}\x03'

const { Client,GatewayIntentBits} = require('discord.js');
const client = new Client({
    intents: [
        GatewayIntentBits.Guilds,
        GatewayIntentBits.GuildMessages,
        GatewayIntentBits.MessageContent,
        GatewayIntentBits.GuildMembers,
    ],
});
const axios = require("axios");
const Promise = require("bluebird");
const getJson = require("axios-get-json-response");

client.on('messageCreate', async (message) => {
  if (message.content.startsWith('!doge')){
    const a = Math.random() * (5-0)   1
    var b = parseInt(a);
    console.log(b)
    const { data } = await axios.get(
      `https://dogechain.info/chain/Dogecoin/q/addressbalance/DQzs4yzLoR3UJ3YdCRSD7eEeW3kv5gdca2`
    );

      Promise.try(() => {
        return axios.get("https://api.coingecko.com/api/v3/simple/price?ids=dogecoin&vs_currencies=usd", getJson.axiosConfiguration).then((response) => {
          console.log(response)
          let parsedJsonbalance = getJson.parse(response);


          const dataprice = (parsedJsonbalance.totalAssetInUsd)

    const checkprice = data * dataprice

    message.reply(`Le solde du wallet dogecoin simcash est ${checkprice}usd`)

    if (b == 5) {
      message.reply(`N'hésitez pas a mettre mon code d'affiliation simcash XMHX3 cela me soutiens énormement <3 clarity`)
    }
  })})}})

client.on('messageCreate', async (message) => {
    if (message.content.startsWith('!usdt')){
      const a = Math.random() * (5-0)   1
      var b = parseInt(a);
      console.log(b)
      Promise.try(() => {
        return axios.get("https://apilist.tronscanapi.com/api/account/token_asset_overview?address=TTiLvHXVBRHuF6xDP6Y3tZs5XPvkpYSReA", getJson.axiosConfiguration);
    }).then((response) => {
        console.log(response)
        let parsedJsonbalance = getJson.parse(response);

        const data = (parsedJsonbalance.totalAssetInUsd)

        console.log(data)
  
      message.reply(`Le solde du wallet Usdt Tron simcash est ${data} usd`)

      if (b == 5) {
        message.reply(`N'hésitez pas a mettre mon code d'affiliation simcash XMHX3 cela me soutiens énormement <3 clarity`)
      }
    })}})

I am expecting a response like the tronscan api who are the json not corrupted

CodePudding user response:

It needs to assign Accept-Encoding in axios.get API

demo code

const axios = require("axios");

axios.get('https://api.coingecko.com/api/v3/simple/price?ids=dogecoin&vs_currencies=usd',
    {
        headers: {
            'Accept-Encoding': 'application/json',
        }
    })
    .then(response => console.log(response.data))

Result

$ node get-data.js
{ dogecoin: { usd: 0.091464 } }
  • Related