0Pricing
Vue Academy · Lesson

HTTP Requests with Axios

Make GET and POST requests to APIs with Axios.

HTTP Requests with Axios is a free Vue Academy lesson on CoddyKit — lesson 1 of 3. You can read the complete lesson below for free — then practise it hands-on in the browser with a built-in code editor and a 24/7 AI tutor. It is part of the Vue Academy learning path, one of 3 lessons in the course, and your progress syncs across the web and the CoddyKit app.

Talking to APIs

Most real apps load data from a backend over HTTP. While the browser has a built-in fetch, many Vue projects use Axios, a popular promise-based HTTP client with a friendlier API, automatic JSON parsing, and powerful configuration options.

Installing Axios

Add Axios to your project with your package manager, then import it where you need it.

npm install axios

Importing Axios

Import the default export to start making requests.

import axios from "axios"

A GET Request

Use axios.get to fetch data. It returns a promise that resolves to a response object; the body is on response.data, already parsed from JSON.

axios.get("https://api.example.com/users")
  .then(response => {
    console.log(response.data)
  })

A POST Request

Use axios.post to send data. The second argument is the request body, which Axios serializes to JSON automatically.

axios.post("https://api.example.com/users", {
  name: "Grace",
  email: "grace@example.com"
})

Using async/await

Promises read more clearly with async/await. Mark the function async and await the request.

async function loadUsers() {
  const response = await axios.get("/api/users")
  return response.data
}

Inside a Vue Method

A common pattern is to call Axios from a component method and store the result in reactive data.

export default {
  data() {
    return { users: [] }
  },
  methods: {
    async fetchUsers() {
      const res = await axios.get("/api/users")
      this.users = res.data
    }
  }
}

Request Config: Query Params

Pass a params object to add query string parameters. Axios builds the URL for you.

axios.get("/api/users", {
  params: { page: 2, limit: 10 }
})
// requests /api/users?page=2&limit=10

Request Config: Headers

Send custom headers, such as an authorization token, through the config object.

axios.get("/api/profile", {
  headers: {
    Authorization: "Bearer " + token
  }
})

Base URL Configuration

Repeating the full URL everywhere is error-prone. Create an Axios instance with a baseURL and shared defaults, then use it throughout your app.

import axios from "axios"

export const api = axios.create({
  baseURL: "https://api.example.com",
  timeout: 5000,
  headers: { "Content-Type": "application/json" }
})

// elsewhere: api.get("/users")

Other HTTP Methods

Axios mirrors the common HTTP verbs: get, post, put, patch, and delete. Use the one that matches the action you want to perform.

api.put("/users/1", { name: "Updated" })
api.patch("/users/1", { name: "Patched" })
api.delete("/users/1")

Quick Check

Test your knowledge of Axios requests.

Recap

Axios is a promise-based HTTP client. Use get, post, put, patch, and delete, pass params and headers through the config object, and prefer async/await for readability. A shared instance via axios.create with a baseURL keeps requests DRY. Next you will fetch data at the right point in a component lifecycle.

Frequently asked questions

Is the “HTTP Requests with Axios” lesson free?

Yes — the full text of “HTTP Requests with Axios” is free to read here on the web, and the Vue Academy course includes 3 lessons in total. To practise it interactively (a built-in code editor and a 24/7 AI tutor) and unlock the rest of the Vue Academy course, upgrade to CoddyKit PRO.

What will I learn in “HTTP Requests with Axios”?

Make GET and POST requests to APIs with Axios. You practise Vue Academy with hands-on code you run directly in the browser, and a 24/7 AI tutor answers your questions as you work through the lesson.

Do I need any experience to start Vue Academy?

No prior experience is required. Vue Academy on CoddyKit is structured for beginners through advanced learners; this is — lesson 1 of 3, so you can start here or from the beginning and move at your own pace.

How long does the “HTTP Requests with Axios” lesson take?

Most CoddyKit lessons take about 5–10 minutes. Each one is bite-sized and interactive, so you make steady progress and pick up exactly where you left off across the web and the app.

Can I write and run code in this Vue Academy lesson?

Yes. Every Vue Academy lesson includes a built-in code editor, so you write and run real code right in your browser and get instant AI feedback — no local setup required.

All lessons in this course

  1. HTTP Requests with Axios
  2. Lifecycle Hooks and Data Fetching
  3. Error Handling and Global Interceptors
← Back to Vue Academy