Axios သည် HTTP requests လုပ်ရန် popular library ဖြစ်သည်။ Fetch API ထက် easier syntax ရှိပြီး additional features များပါဝင်သည်။
💡 Installation: npm install axios
Features:
• Automatic JSON transformation
• Request/Response interceptors
• Better error handling
• Cancel requests
javascript
// Axios usage pattern (conceptual)
// Simulated axios-like API
const axios = {
get(url) {
return Promise.resolve({
status: 200,
data: { id: 1, title: "Sample Post" }
});
},
post(url, data) {
return Promise.resolve({
status: 201,
data: { id: 2, ...data }
});
}
};
// GET request
axios.get("https://api.example.com/posts/1")
.then(response => {
console.log("Status:", response.status);
console.log("Data:", response.data);
});
// POST request
axios.post("https://api.example.com/posts", {
title: "New Post",
body: "Content here"
}).then(response => {
console.log("Created:", response.data);
});
// With async/await
async function fetchData() {
try {
const response = await axios.get("https://api.example.com/posts");
console.log("Fetched:", response.data);
} catch (error) {
console.error("Error:", error);
}
}You should see
Status: 200 Data: {id: 1, title: 'Sample Post'} Created: {id: 2, title: 'New Post', body: 'Content here'}