posting data with Axios
Try this one also and replace
baseURLwith your own hostname url
import axios from 'axios'
let var1 = 'firstName'
let value1 = 'Fred'
let var2 = 'lastName'
let value2 = 'Flinstone'
const api = axios.create({baseURL: 'http://example.com'})
api.post('/user/12345', {
var1: value1,
var2: value2
})
.then(res => {
console.log(res)
})
.catch(error => {
console.log(error)
})
Try this it works for me
const obj = {
firstName: Fred,
lastName: Flinstone
}
axios
.post(
"url",
this.obj,
)
.then(response => {
console.log(response)
})
.catch(error => {
console.log(error);
});
You can create your own object and pass it to your data request like this:
var obj = {
[myKey]: value,
}
or
var obj = {};
obj['name'] = value;
obj['anotherName'] = anotherValue;
Creating object with dynamic keys
Dynamically Add Variable Name Value Pairs to JSON Object
edited: how to post request
const profile = {};
//...fill your object like this for example
profile[key] = value;
axios.post('profile/student', profile)
.then(res => {
return res;
});