Vue resource tutorial- How to make http requests
In this tutorial, we are going to learn about how to make get requests and post requests in vuejs using vue resource package.
Creating Vue project
Let’s create a new Vue project by using the vue cli.
vue create vue-httpNow change your current working directory by running the below command in your terminal.
cd vue-httpInstalling and configuring vue resource
Now we are going to install the vue-resource package by running the following command.
npm i vue-resourceOnce you successfully installed the vue-resource package, now we need to configure this package by adding below-highlighted lines to the main.js file.
import Vue from "vue";
import App from "./App.vue";
import VueResource from 'vue-resource';
Vue.use(VueResource);
Vue.config.productionTip = false;
new Vue({
render: h => h(App)
}).$mount("#app");By adding this configuration VueResource provides us a global $http object, which helps us to make http requests anywhere from our Vue app.
Making GET request
For learning purposes, we are using json placeholder API for making http requests.
GET request method is used to fetch the data from the backend API, let’s see an example.
<template>
<div id="app">
<ul>
<li>Id : {{post.id}}</li> <li>Title: {{post.title}}</li> </ul>
</div>
</template>
<script>
export default {
data: function() {
return {
post: "" };
},
created() {
this.$http.get("https://jsonplaceholder.typicode.com/posts/1") .then(res => { this.post = res.body; }); }
};
</script>In the above code, we are making a get request inside created() lifecycle hook once the data comes back from the API we are updating the post property with the response.
Making POST request
The POST request method is used to send the data to the backend API, let’s see an example.
<template>
<div id="app">
<form> <input v-model="title" placeholder="Title" required /> <textarea v-model="body" placeholder="Body" required /> <button @click.prevent="sendPost()">Submit post</button> </form> <ul>
<li>Id : {{post.id}}</li>
<li>Title: {{post.title}}</li>
</ul>
</div>
</template>
<script>
export default {
data: function() {
return {
post: "",
title: "", body: "" };
},
methods: {
sendPost() { const postData = { title: this.title, body: this.body }; this.$http .post("https://jsonplaceholder.typicode.com/posts", postData) .then(res => { console.log(res.body); }); }
},
created() {
this.$http.get("https://jsonplaceholder.typicode.com/posts/1")
.then(res => {
this.post = res.body;
});
}
};
</script>In the above code, this.$http.post() method takes two arguments, the first argument is the api url and second argument is the data we need to send to the api.
Setting root url
In vue resource we can also set global root url instead of adding a full url to every request.
Open your main.js file and add the below code.
import Vue from "vue";
import App from "./App.vue";
import VueResource from "vue-resource";
Vue.use(VueResource);
Vue.http.options.root = "https://jsonplaceholder.typicode.com/";
Vue.config.productionTip = false;
new Vue({
render: h => h(App)
}).$mount("#app");Now we need to just add paths inside our GET or POST request, which are appended to this root url.
<template>
<div id="app">
<form>
<input v-model="title" placeholder="Title" required>
<textarea v-model="body" placeholder="Body" required/>
<button @click.prevent="sendPost()">Submit post</button>
</form>
<ul>
<li>Id : {{post.id}}</li>
<li>Title: {{post.title}}</li>
</ul>
</div>
</template>
<script>
export default {
data: function() {
return {
post: "",
title: "",
body: ""
};
},
methods: {
sendPost() {
const postData = { title: this.title, body: this.body };
this.$http.post("posts", postData).then(res => { console.log(res.body);
});
}
},
created() {
this.$http.get("posts/1").then(res => { this.post = res.body;
});
}
};
</script>

