Now that we have made our form, it’s time to introduce the network request to the GitHub API.
To do so, we are going to use the Axios library.
It’s a nice useful and lightweight library to handle network requests.
Go back to the command line (you might need to quit the Vue app to do so, using ctrl-C) and install it locally using npm install axios
.
Import it in your Form component script code:
import axios from 'axios'
GitHub offers us a public API to ask user’s information. We can call it using the HTTP GET method on the URL https://api.github.com/users/<username>
.
Let’s replace the alert(this.username)
line with some network code!
We call
axios.get(`https://api.github.com/users/${this.username}`)
The syntax uses template literals to embed the this.username
variable value in the string.
This returns a promise, so we can use then()
to process the result:
axios.get(`https://api.github.com/users/${this.username}`).then(resp => {
//...
})
At this point resp.data
contains the response data we are looking for, and we can process it.