Follow

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use
Contact

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

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 documentation for axios, but it doesn’t seem to work.

This is my code:

var data = '{\n    "country": "romania"\n}';

    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);
        });

It however works with postman:
enter image description here

MEDevel.com: Open-source for Healthcare and Education

Collecting and validating open-source software for healthcare, education, enterprise, development, medical imaging, medical records, and digital pathology.

Visit Medevel

>Solution :

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%20send%20multipart%20form%20data,using%20the%20append()%20method.

Add a comment

Leave a Reply

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use

Discover more from Dev solutions

Subscribe now to keep reading and get access to the full archive.

Continue reading