Published: July 23 2020

Vue + Axios - HTTP GET Request Examples

Below is a quick set of examples to show how to send HTTP GET requests from Vue to a backend API using the axios HTTP client which is available on npm.

Other HTTP examples available:


Installing axios from npm

With the npm CLI: npm install axios

With the yarn CLI: yarn add axios


Simple GET request using axios

This sends an HTTP GET request from Vue to the npm api to search for all vue packages using the query q=vue, then assigns the total returned in the response to the component data property totalVuePackages so it can be displayed in the component template.

created() {
  // Simple GET request using axios
  axios.get("https://api.npms.io/v2/search?q=vue")
    .then(response => this.totalVuePackages = response.data.total);
}

Example Vue component at https://codesandbox.io/s/vue-axios-http-get-request-examples-ei7l8?file=/app/GetRequest.vue


GET request using axios with async/await

This sends the same GET request from Vue using axios, but this version uses an async function and the await javascript expression to wait for the promises to return (instead of using the promise then() method as above).

async created() {
  // GET request using axios with async/await
  const response = await axios.get("https://api.npms.io/v2/search?q=vue");
  this.totalVuePackages = response.data.total;
}

Example Vue component at https://codesandbox.io/s/vue-axios-http-get-request-examples-ei7l8?file=/app/GetRequestAsyncAwait.vue


GET request using axios with error handling

This sends a GET request from Vue to an invalid url on the npm api then assigns the error message to the errorMessage component data property and logs the error to the console.

created() {
  // GET request using axios with error handling
  axios.get("https://api.npms.io/v2/invalid-url")
    .then(response => this.totalVuePackages = response.data.total)
    .catch(error => {
      this.errorMessage = error.message;
      console.error("There was an error!", error);
    });
}

Example Vue component at https://codesandbox.io/s/vue-axios-http-get-request-examples-ei7l8?file=/app/GetRequestErrorHandling.vue


GET request using axios with set HTTP headers

This sends the same GET request again from Vue using axios with the HTTP Content-Type header set to application/json.

created() {
  // GET request using axios with set headers
  const headers = { "Content-Type": "application/json" };
  axios.get("https://api.npms.io/v2/search?q=vue", { headers })
    .then(response => this.totalVuePackages = response.data.total);
}

Example Vue component at https://codesandbox.io/s/vue-axios-http-get-request-examples-ei7l8?file=/app/GetRequestSetHeaders.vue

 


Subscribe or Follow Me For Updates

Subscribe to my YouTube channel or follow me on Twitter, Facebook or GitHub to be notified when I post new content.

Other than coding...

I'm currently attempting to travel around Australia by motorcycle with my wife Tina on a pair of Royal Enfield Himalayans. You can follow our adventures on YouTube, Instagram and Facebook.


Need Some Vue Help?

Search fiverr to find help quickly from experienced Vue developers.



Supported by