How can I send a POST request using axios to a REST API?
I'm experiencing issues with incorrect headers when making GET requests, and I'm not sure why. Additionally, I've found that the documentation for axios doesn't always work properly.
Here's an example of a successful GET request: Dynamic host in axios
const configAxios = {
headers: {
'Content-Type': 'application/json; charset=UTF-8',
'Access-Control-Allow-Origin': '*',
},
};
const data = JSON.stringify({
cardData: this.cardData.brand,
});
axios.post('api/products', {
data,
},
configAxios,
)
.then((req) => {
this.data = req.data;
console.log(req);
})
.catch((err) => {
console.warn('error during http call', err);
});
To access the API host, you need to do the following:
p.s.: This method is necessary for it to work correctly.
const baseURL = 'http://localhost:8080';
if (typeof baseURL !== 'undefined') {
Vue.axios.defaults.baseURL = baseURL;
}
- Axios version: e.g.: v0.16.2
- Environment: e.g.: node v8.9.4, chrome 64.0.3282.119, Ubuntu 16.04
- Symfony 4.0.4
- Vue.js 2.4.2
- vue-axios 2.0.2