HTTP methods: GET, POST and DELETE

GET: Requests data from a resource

POST: Submits data to be processed to a specified resource

DELETE: Deletes the resource

GET: pulls

POST: creates

DELETE: removes

HTTP Status Codes

* 100-level - indicates that the request was recieved and understood. But, nothing has happened yet.

* 200-level - indicates the action requested by the client was recieved understood, accepted and processed successfully.

* 300-level - Indicate the client must take additional action to complete the request, often used for redirection

* 400-level - The error seems to have been caused by the client

* 500-level - The server failed to fulfill a valid request

* 100-level - hold on

* 200-level - here you go

* 300-level - go away

* 400-level - you messed up

* 500-level - server's messed up

Standard AJAX request

                            
                                // Standard AJAX request to get a response from an API
                                var a = new XMLHttpRequest();
                                a.addEventListener('readystatechange', function(r) {
                                    // r is the captured event
                                    if (r.target.status === 200) { // 200 means successfull response
                                        console.log(r.target.response);
                                    }
                                });
                                a.open('GET', 'https://api.github.com/users/KapadiaShubham', true);
                                a.send();
                            
                        

Promise states

* Pending: incomplete

* Fulfilled: complete

* Rejected: failed

fetch() - Calling an API by Promises method

                            
                                // Fetch API - a modern way
                                fetch('https://api.github.com/users/KapadiaShubham')
                                    .then(function(r) {
                                        console.log(r.status);
                                        return r.json();
                                    })
                                    .then(function(j) {
                                        console.log(j);
                                    })
                            
                        

Calling an API Example

                                            
                                                <html>
<head>
    <title>JSON GitHub Activity</title>
</head>

<body>
    <div class="container">
        <h1>GitHub Profile Hunt</h1>
        <h3 id="loader">Loading...</h3>
        <div><img id="avatar" src="" /></div>
        <div id="name"></div>
        <div id="username"></div>
        <div id="location" ≥</div>
            <div id="bio"></div>
            <div>
                <div id="count"></div>
                <ul id="list">
                </ul>
            </div>
            <input. type="text" placeholder="GitHub username">
                <button>Get GitHub Profile</button>
        </div>
        <script src="jsonhandling.js"></script>
</body>

</html>
                                            
                                        
                                            
                                                container {
                                                    text-align: center;
                                                }
                                                img {
                                                    margin: 5px;
                                                    width: 20px;
                                                }
                                                #avatar {
                                                    width: 150px;
                                                }
                                                ul {
                                                    padding: 0;
                                                    display: flex;
                                                    justify-content: center;
                                                    list-style-type: none;
                                                }
                                            
                                        
                            
                                var response = null;
var followers = null;

document.getElementsByTagName('button')[0].addEventListener('click', function(r) {
    getUser(document.getElementsByTagName('input')[0].value);
});

function getUser(name) {
    fetch('https://api.github.com/users/' + name)
        .then(function(r) {
            return r.json();
        })
        .then(function(j) {
            response = j;
            assignValues();
            getFollowers(j.followers_url);
        })
}

function assignValues() {
    document.getElementById('loader').style = 'display: none';

    document.getElementById('avatar').src = response.avatar_url;
    document.getElementById('name').innerText = response.name;
    document.getElementById('username').innerText = response.login;
    document.getElementById('location').innerText = response.location;
    document.getElementById('bio').innerText = response.bio;
    document.getElementById('count').innerText = 'Followers:' + response.followers;
}

function getFollowers(url) {
    fetch(url)
        .then(function(r) {
            return r.json();
        })
        .then(function(f) {
            followers = f;
            console.log(f);
            listFollowers();
        });
};

function listFollowers() {
    followers.forEach(function(f) {
        var li = document.createElement('li');
        li.innerHTML = '' + '' + '"';
        document.getElementById('list').appendChild(li);
    });
}
                            
                        
                            
                                
                                    FrontEnd: HTML / CSS / JS
                                    How to make a search from a list - 
                                    https://www.w3schools.com/howto/howto_js_search_menu.asp