Published:

Vue + Axios - HTTP POST Request Examples

Below is a quick set of examples to show how to send HTTP POST 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 POST request with a JSON body using axios

This sends an HTTP POST request to the Reqres api which is a fake online REST api that includes a generic /api/<resource> route that responds to POST requests for any <resource> with the contents of the post body and a dynamic id property. This example sends an article object to the /api/articles route and then assigns the id from the response to the vue component data property articleId so it can be displayed in the component template.

created() {
  // Simple POST request with a JSON body using axios
  const article = { title: "Vue POST Request Example" };
  axios.post("https://reqres.in/api/articles", article)
    .then(response => this.articleId = response.data.id);
}

Example Vue component at https://codesandbox.io/s/vue-axios-http-post-request-examples-ecqqn?file=/app/PostRequest.vue


POST request using axios with async/await

This sends the same POST 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() {
  // POST request using axios with async/await
  const article = { title: "Vue POST Request Example" };
  const response = await axios.post("https://reqres.in/api/articles", article);
  this.articleId = response.data.id;
}

Example Vue component at https://codesandbox.io/s/vue-axios-http-post-request-examples-ecqqn?file=/app/PostRequestAsyncAwait.vue


POST request using axios with error handling

This sends a POST request from Vue using axios to an invalid url on the api then assigns the error message to the errorMessage component state property and logs the error to the console.

created() {
  // POST request using axios with error handling
  const article = { title: "Vue POST Request Example" };
  axios.post("https://reqres.in/invalid-url", article)
    .then(response => this.articleId = response.data.id)
    .catch(error => {
      this.errorMessage = error.message;
      console.error("There was an error!", error);
    });
}

Example Vue component at https://codesandbox.io/s/vue-axios-http-post-request-examples-ecqqn?file=/app/PostRequestErrorHandling.vue


POST request using axios with set HTTP headers

This sends the same POST request again from Vue using axios with a couple of headers set, the HTTP Authorization header and a custom header My-Custom-Header.

created() {
  // POST request using axios with set headers
  const article = { title: "Vue POST Request Example" };
  const headers = { 
    "Authorization": "Bearer my-token",
    "My-Custom-Header": "foobar"
  };
  axios.post("https://reqres.in/api/articles", article, { headers })
    .then(response => this.articleId = response.data.id);
}

Example Vue component at https://codesandbox.io/s/vue-axios-http-post-request-examples-ecqqn?file=/app/PostRequestSetHeaders.vue

 

Subscribe or Follow Me For Updates

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

 


Supported by