Richieste POST
Come eseguire richieste POST con Axios
Eseguire una richiesta POST
JSON
axios
.post("/user", {
firstName: "Fred",
lastName: "Flintstone",
})
.then(function (response) {
console.log(response);
})
.catch(function (error) {
console.log(error);
});
Eseguire più richieste concorrenti
function getUserAccount() {
return axios.get("/user/12345");
}
function getUserPermissions() {
return axios.get("/user/12345/permissions");
}
const [acct, perm] = await Promise.all([
getUserAccount(),
getUserPermissions(),
]);
// OPPURE
Promise.all([getUserAccount(), getUserPermissions()]).then(function ([
acct,
perm,
]) {
// ...
});
Inviare un form HTML come JSON
const { data } = await axios.post("/user", document.querySelector("#my-form"), {
headers: {
"Content-Type": "application/json",
},
});
Form
- Multipart (
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,
},
{
headers: {
"Content-Type": "multipart/form-data",
},
}
);
- Form con URL codificato (
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",
},
}
);