Home > Net >  How do I send a post request with axios? (CountriesNow API)
How do I send a post request with axios? (CountriesNow API)

Time:11-24

I'm trying to get the cities from Romania using the countriesnow api, however, I am always receiving the following message: {"error":true,"msg":"missing param (country)"} I've followed the enter image description here

CodePudding user response:

var data = {
  country: "romania"
};

And the same to all.

If it doesn't work for you, I suggest to append the data to form data like the following.

const data = new FormData();
data.append('country','romania');

// same

Please refer to this. https://masteringjs.io/tutorials/axios/axios-multi-form-data#:~:text=To send multipart form data,using the append() method.

CodePudding user response:

You need to send the data as a JavaScript object instead of a JSON string.

    var data = {"country": "romania"};

    axios({
        method: 'post',
        headers: {},
        url: 'https://countriesnow.space/api/v0.1/countries/cities',
        data: data
    })
    .then((response) => {
        console.log(response.data);
    })
    .catch((error) => {
        console.log(error);
    });
  • Related