Axios GET request is returned a wrong response (checked with Postman)

Viewed 52

I'm a very newbie in JS (I started to learn 3 days ago) and can't find what happened wrong in my code.

I'm trying to get the API response from the following URL: https://api.dictionaryapi.dev/api/v2/entries/en/dog

If the request is made with Postman the response is right. That's means, the dict returned in JSON has the field "meaning" and the sub-fields "partOfSpeech", "definitions", ...

    "meanings": [
        {
            "partOfSpeech": "noun",
            "definitions": [
                {
                    "definition": "A mammal, Canis familiaris or Canis lupus familiaris, that has been domesticated for thousands of years, of highly variable appearance due to human breeding.",
                    "synonyms": [],
                    "antonyms": [],
                    "example": "The dog barked all night long."

But if I try to get the response in my code with Axios, the field "meaning" has the wrong structure, and seems that some parts of the response are lost.

The structure returned by axios:

meaning: { noun: [Array], verb: [Array] }

In "meaning", sub-fields like "partOfSpeech" doesn't exist!

 noun: [
{
  definition: 'Meat from a dog eaten as food.',
  synonyms: [],
  antonyms: [],
  example: 'We visited South Korea this time around, where we ate dog meat for the first time.'
},
{
  definition: 'Meat prepared to be given to a dog as food.',
  synonyms: [],
  antonyms: []
},

Below is a part of my code

const axios = require('axios').default;
async function getInfo(URL, params) {
    try {
        const response = await axios.get(URL, params);
        return response.data;
    } catch (error) {
        return error;
    }
}

function searchWordDefinition(word) {
    return getInfo(dict_URL + word);
}

var dict_URL = 'https://api.dictionaryapi.dev/api/v1/entries/en/';
var words = ["dog"];
1 Answers

Your Axios script uses the V1 of the API, your POSTMAN example uses V2.

Change your dict_URL to https://api.dictionaryapi.dev/api/v2/entries/en/ and you should be good.

Related