POST अनुरोध
Axios के साथ POST अनुरोध कैसे करें
POST अनुरोध निष्पादित करना
JSON
axios.post('/user', {
पहला नाम: 'Fred',
अंतिम नाम: 'Flintstone'
})
.then(function (response) {
console.log(response);
})
.catch(function (error) {
console.log(error);
});
एक साथ कई अनुरोध निष्पादित करना
फ़ंक्शन getUserAccount() {
return axios.get('/user/12345');
}
फ़ंक्शन getUserPermissions() {
return axios.get('/user/12345/permissions');
}
const [acct, perm] = await Promise.all([getUserAccount(), getUserPermissions()]);
/ या
Promise.all([getUserAccount(), getUserPermissions()])
.then(फ़ंक्शन ([acct, perm]) {
/ ...
});
एक HTML फ़ॉर्म को JSON के रूप में पोस्ट करें
const {data} = await axios.post('/user', document.querySelector('#my-form'), {
हेडर: {
'Content-Type': 'application/json'
}
})
फ़ॉर्म
- मल्टीपार्ट (
multipart/form-data)
const {data} = await axios.post('https://httpbin.org/post', {
firstName: 'Fred',
lastName: 'Flintstone',
orders: [1, 2, 3],
photo: document.querySelector('#fileInput').files
}, {
हेडर: {
'Content-Type': 'multipart/form-data'
}
}
)
- URL एनकोडेड फ़ॉर्म (
application/x-www-form-urlencoded)
const {data} = await axios.post('https://httpbin.org/post', {
firstName: 'Fred',
lastName: 'Flintstone',
orders: [1, 2, 3]
}, {
headers: {
'Content-Type': 'application/x-www-form-urlencoded'
}
})