These are actions that the browser can perform on the server
we want to let say get data from the server; post data to the server; delete data from the server; update data on the server these are the types of HTTP requests
the request are made to API endpoints
we make a request to gitHub use the API endpoints https://api.github.com/users/qasimTalkin
The data is returned in the form of a JSON object
Chrome Network Tab
The network tab is where we can see the requests that are made to the server
In Header we can see the type of request that is made
In Body we can see the data that is sent to the server
In Response we can see the data that is returned from the server
XMLHttpRequest is a way to make a request to the server, this is built into js let request = new XMLHttpRequest()
to set up request GETrequest.open('GET', 'https://api.github.com/users/qasimTalkin')
to send the request request.send()
add event listener to check state of our request
ready state 0 means request is not initialized, 1 means request has been set up, 2 means request has been sent, 3 means request is in process, 4 means request is complete
let request = new XMLHttpRequest();
request.addEventListener('readystatechange', function() {
if (this.readyState===4){
console.log( JSON.parse(this.response))
}
});
request.open('GET', 'https://api.github.com/users/qasimtalkin');
request.send();
request.readyState 4 is not enough
we need to check the status of the request with ready state
let request = new XMLHttpRequest();
request.addEventListener('readystatechange', function() {
if (this.readyState===4 && this.status===200){
console.log( JSON.parse(this.response))
}
});
creating function to make request
let getMyData() be a function that makes a request to the server
within getMyData we can set up the request, and send it
we can pass a callback function to getMyData, this call back will be called when the request is complete
let getMyData = function(callback) {
let request = new XMLHttpRequest();
request.addEventListener('readystatechange', function() {
if (request.readyState===4 && request.status===200){
callback(undefined, request.response)
} else {
callback('error', undefined)
}
});
request.open('GET', 'https://api.github.com/users/qasimtalkin');
request.send();
}
getMyData();
response to json
we can convert the response to json with JSON.parse(response)
the data by default is a string, in order to use the data we need to convert it to json first
Example
let response = JSON.parse(request.response);
// multiple ways to loop through the JSON data
response.forEach(function(item) {
console.log(item.name);
});
for (item of response) {
console.log(item.name);
}
for (let i = 0; i < response.length; i++) {
console.log(response[i].name);
}
callBack Hell
call back hell is when we have nested callbacks, making request to one API and using that data to make another request to another API
for example we have function that gets all users with an git account and then we have a function that gets all the repositories for each user and then we have a function that gets all the issues for each repository and then we have a function that gets all the comments for each issue
it chains promises together in clean and easier way
we can make the function async and then we can use await to wait for the promise to resolve
bundles up all the async code in one function
let asyncFunction = async () => {
let response = await fetch('https://api.github.com/users/qasimtalkin');
let data = await response.json();
console.log(data);
}
Throwing erros in Async and Await
we can throw an error in the async function with throw new Error('error message')
we can catch the error in the async function with try { } catch(err) { }
for response object we need to check the status code and throw an error if it is not 200
let asyncFunction = async () => {
let response = await fetch('https://api.github.com/users/qasimtalkin');
if (response.status !== 200) {
thrownewError('Request failed with status code ' + response.status);
}
let data = await response.json();
console.log(data);
}