Vue Axios Request

Posted on  by admin
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:. Vue + Axios: GET. Vue + Fetch: GET, POST. React + Fetch: GET, POST, PUT, DELETE.

React + Axios: GET, POST, PUT, DELETE.

Angular: GET, POST, PUT, DELETE. Blazor WebAssembly:GET, POST. Axios: GET, POST, PUT, DELETE. Fetch: GET, POST, PUT, DELETE. With the npm CLI: npm install axios. With the yarn CLI: yarn add axios. This sends an HTTP POST request to the Reqres api which is a fake online REST api that includes a generic /api/ route that responds to POST requests for any 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.

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

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).

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

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.

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

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.
MichaelBMichaelB
1,060
2

Other HTTP examples available:.

Vue + Axios: POST. Vue + Fetch: GET, POST.

React + Fetch: GET, POST, PUT, DELETE. React + Axios: GET, POST, PUT, DELETE. Angular: GET, POST, PUT, DELETE. Blazor WebAssembly:GET, POST. Axios: GET, POST, PUT, DELETE. Fetch: GET, POST, PUT, DELETE. With the npm CLI: npm install axios. With the yarn CLI: yarn add 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.

BertBert
76.1k4

Example Vue component at https://codesandbox.io/s/vue-axios-http-get-request-examples-ei7l8?file=/app/GetRequestAsyncAwait.vue. 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.

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

This sends the same GET request again from Vue using axios with the HTTP Content-Type header set to application/json. Example Vue component at https://codesandbox.io/s/vue-axios-http-get-request-examples-ei7l8?file=/app/GetRequestSetHeaders.vue.

Axios is a promise-based HTTP Client Javascript library for Node.js and Browser.
0

Not the answer you're looking for? Browse other questions tagged javascriptvue.jsvuejs2axios or ask your own question.