npm i axios
<script src="https://cdnjs.cloudflare.com/ajax/libs/axios/1.0.0-alpha.1/axios.min.js"></script>
const res = await axios.get("URL");
<!-- "get data"-->
console.log(res.data);
<!-- "get response" state -->
console.log(res.status);
axios.get("URL")
.then(() => {})
.catch(error => {console.log(error)});
// fetch posts from jsonplaceholder API via Axios
axios.get("https://jsonplaceholder.typicode.com/posts")
.then(res => console.log(res.data))
.catch(error => console.log(error))
axios.post("URL" , {"the object that you want to send"});
// send new data into jsonplaceholder api via POST method
axios.post("https://jsonplaceholder.typicode.com/posts", {
title: "test title",
body: "this is post body"
}).then(res => console.log(res.data))
.catch(error => console.log(error));
what is the difrent between PATCH and PUT?
PATCH: update only data parts that are included in an object that we sent.
PUT: replace old data with the new one.
axios.patch("URL" , {"object contains only the new values"});
// update todo title and complete state
axios.patch("https://jsonplaceholder.typicode.com/todos/1", {
"title": "todo after update all field",
"completed": true
}).then(res => console.log(res.data))
.catch(error => console.log(error));
axios.put("URL" , {"new object values"});
// replace objecct 1 in todo list
axios.put("https://jsonplaceholder.typicode.com/todos/1", {
"userId": 2,
"id": 1,
"title": "todo after update all field",
"completed": true
}).then(res => console.log(res.data))
.catch(error => console.log(error));
axios.delete("URL");
// delete todo in url https://jsonplaceholder.typicode.com/todos/1
axios.delete("https://jsonplaceholder.typicode.com/todos/1")
.then(res => console.log(res))
.catch(error => console.log(error));
axios.all([
axios.get("URL1"),
axios.get("URL2")
]).then(axios.spread((url1Res , url2Res)
=> console.log(url1Res , url2Res)));
// more than one request
axios.all([
axios.get("https://jsonplaceholder.typicode.com/posts"),
axios.get("https://jsonplaceholder.typicode.com/todos")
]).then(axios.spread((posts, todos) => console.log(posts, todos)));
when you need to use custom headers?
example: when the api need authentication
axios.get("URL" , {
headers:{
KEY:`${API_KEY}`
},
params:{"Params to send for the API with request"}
})
axios.get("https://tomtom/api/location" , {
headers:{
KEY:`${process.env.TOMTOM_DEVELOPER_KEY}`
},
params:{
lat:34.25,
lng:32.21
}
})
Login pageSignup pageHome pageList frindesAdd friend + invite friendJoin puplic gameCreate new gameGame roomGame show ruleGame room silent statusGame room discussionGame room voteGame room vote resultVictoryGame points + repeat game or back homeRank page
Nov 30, 2023grades
Sep 2, 2023index -> charts and statistics
Jul 24, 2023CORE MODULES GLOBALS global classes example (Math class) cont val = Math.abs(-5); global functions example (fetch) fetch('https://jsonplaceholder.typicode.com/todos/1') .then(response => response.json()) .then(json => console.log(json))
Jun 4, 2023or
By clicking below, you agree to our terms of service.
New to HackMD? Sign up