Vue Resource HTTP Client

1.5.3 · abandoned · verified Wed Apr 22

vue-resource is an HTTP client plugin for Vue.js, designed for making web requests and handling responses using XMLHttpRequest or JSONP. It supports the Promise API, URI Templates, and interceptors for both requests and responses. The package, currently at version 1.5.3 (last updated in 2021), was primarily built for Vue 1.x and Vue 2.x applications. It boasts compatibility with modern browsers (Firefox, Chrome, Safari, Opera, IE9+) and a compact size (14KB, 5.3KB gzipped). While it was once considered a de-facto 'official' HTTP client, its maintenance has largely ceased, especially with the advent of Vue 3, which encourages the use of alternatives like Axios or the native Fetch API. Its release cadence was irregular towards the end of its active lifecycle, with its last update occurring several years ago.

Common errors

Warnings

Install

Imports

Quickstart

This quickstart demonstrates how to install `vue-resource` as a Vue plugin, perform basic GET and POST HTTP requests within a Vue component's lifecycle hook, and handle both successful responses and errors by updating component data.

import Vue from 'vue';
import VueResource from 'vue-resource';

Vue.use(VueResource);

new Vue({
  el: '#app',
  data: {
    message: 'Loading data...',
    someData: null,
    error: null,
  },
  mounted() {
    // Example GET request
    this.$http.get('https://jsonplaceholder.typicode.com/todos/1')
      .then(response => {
        // Handle success
        this.message = 'Data loaded!';
        this.someData = response.body; // response.body contains the parsed data
      }, response => {
        // Handle error
        this.message = 'Error loading data.';
        this.error = response.statusText;
        console.error('Request failed:', response);
      });

    // Example POST request
    this.$http.post('https://jsonplaceholder.typicode.com/posts', { title: 'foo', body: 'bar', userId: 1 })
      .then(response => {
        console.log('POST successful:', response.body);
      })
      .catch(error => {
        console.error('POST failed:', error);
      });
  },
  template: `
    <div id="app">
      <h1>Vue Resource Example</h1>
      <p>{{ message }}</p>
      <div v-if="someData">
        <h2>Fetched Data (GET)</h2>
        <pre>{{ JSON.stringify(someData, null, 2) }}</pre>
      </div>
      <div v-if="error">
        <p style="color: red;">Error: {{ error }}</p>
      </div>
    </div>
  `,
});

view raw JSON →