Baike.dev
All toolsOnline toolsAI codingTrendingOpen sourceNewsSubmit
Log in
< Back to tools
A

axios-tutorial-react

> 编程语言
Open source

Axios Tutorial React Course My React Course Support Find the App Useful? You can always buy me a coffee Docs Axios Docs Install sh npm inst…

99 stars0 likes2 views
WebsiteGitHub

About

Axios Tutorial React Course My React Course Support Find the App Useful? You can always buy me a coffee Docs Axios Docs Install sh npm inst…

Axios Tutorial

React Course

My React Course

Support

Find the App Useful? You can always buy me a coffee

Docs

Axios Docs

Install

bash
npm install axios
javascript

First Request

  • import axios

  • axios.get(url)

  • axios.post(url)

  • axios.patch/put(url)

  • axios.delete(url)

  • default get axios(url)

  • returns a promise

  • response data located in data property

  • error in error.response

javascript
import axios from 'axios';

const fetchData = async () => {
  try {
    // axios.get(), axios.post(),axios.put(), axios.delete()
    const response = await axios(url);

    console.log(response);
  } catch (error) {
    console.log(error.response);
  }
};

Headers

  • second argument

  • axios.get(url,{})

  • third argument in requests with data

  • axios.post(url,{data},{})

javascript
const fetchDadJoke = async () => {
  try {
    const { data } = await axios(url, {
      headers: {
        Accept: 'application/json',
      },
    });
    // console.log(data);
    setJoke(data.joke);
  } catch (error) {
    console.log(error.response);
  }
};

Post Request

  • send data to the server
  • axios.post(url, { data })
  • more options (auth header) - axios.post(url, { data },{})
javascript
try {
  const resp = await axios.post(url, { data });
} catch (error) {
  console.log(error.response.data);
}

Global Defaults

javascript
axios.defaults.headers.common['Accept'] = 'application/json';
axios.defaults.baseURL = 'https://api.example.com';
axios.defaults.headers.common['Authorization'] = AUTH_TOKEN;
axios.defaults.headers.post['Content-Type'] =
  'application/x-www-form-urlencoded';

Custom Instance

javascript
const authFetch = axios.create({
  baseURL: 'https://course-api.com',
  headers: {
    Accept: 'application/json',
  },
});

Interceptors

  • global and custom
javascript
authFetch.interceptors.request.use(
  (request) => {
    request.headers.common['Accept'] = `application/json`;
    console.log('request sent');
    // must return request
    return request;
  },
  (error) => {
    return Promise.reject(error);
  }
);

authFetch.interceptors.response.use(
  (response) => {
    console.log('got response');
    return response;
  },
  (error) => {
    console.log(error.response);
    if (error.response.status === 404) {
      // do something
      console.log('NOT FOUND');
    }
    return Promise.reject(error);
  }
);

Issues· 0 open

View all issuesOpen on GitHub

No open issues yet, or sync has not completed.

> Tags

JavaScript

No comments yet. Be the first to share.

> Details

PublishedAug 1, 2026
UpdatedSep 18, 2026
Category编程语言
PricingOpen source

> Related tools

T
TypeScript
JavaScript 的超集,为前端与全栈提供静态类型
P
Python
通用编程语言,广泛用于 Web、数据与 AI
G
Go
Google 推出的简洁高效系统语言