# axios

***

**1. GET Request with Query Parameters:**

```js
axios.get('https://example.com/api/users', {
  params: {
    name: 'John',
    age: 30
  }
});
```

**2. POST Request with JSON Data:**

```js
axios.post('https://example.com/api/users', {
  name: 'Jane',
  age: 35
});
```

**3. PUT Request to Update Resource:**

```js
axios.put('https://example.com/api/users/1', {
  name: 'John',
  age: 40
});
```

**4. DELETE Request:**

```js
axios.delete('https://example.com/api/users/1');
```

**5. Request with Authorization Header:**

```js
const token = 'eyJhbGciOiJIUzI1NiIsInR5cCI6IkpXVCJ9...';
axios.get('https://example.com/api/users', {
  headers: { Authorization: `Bearer ${token}` }
});
```

**6. Intercepting Requests and Responses:**

```js
axios.interceptors.request.use(config => {
  // Do something before the request is sent
  return config;
});

axios.interceptors.response.use(response => {
  // Do something with the response data
  return response;
});
```

**7. Concurrency:**

```js
const requests = [
  axios.get('https://example.com/api/users/1'),
  axios.get('https://example.com/api/users/2')
];

axios.all(requests).then(axios.spread((user1, user2) => {
  // Do something with both users
}));
```

**8. Handling Errors:**

```js
axios.get('https://example.com/api/users/1')
  .then(response => {
    // Request successful
  })
  .catch(error => {
    // Request failed
  });
```

**9. Timeouts:**

```js
axios.get('https://example.com/api/users/1', {
  timeout: 5000
});
```

**10. Progress Events:**

```js
const progress = axios.get('https://example.com/api/users/1', {
  onDownloadProgress: progressEvent => {
    // Progress event occurred
  }
});
```

**11. Canceling a Request:**

```js
const cancelToken = axios.CancelToken.source();

const request = axios.get('https://example.com/api/users/1', {
  cancelToken: cancelToken.token
});

// Later, cancel the request
cancelToken.cancel();
```

**12. Setting Custom Header:**

```js
axios.defaults.headers.common['X-Custom-Header'] = 'value';
```

**13. Using Proxy:**

```js
const proxy = {
  host: 'localhost',
  port: 8080
};

const config = {
  proxy
};

axios.create(config);
```

**14. Basic Authentication:**

```js
const auth = {
  username: 'username',
  password: 'password'
};

const config = {
  auth
};

axios.create(config);
```

**15. Using Axios with Express.js:**

```js
const express = require('express');
const axios = require('axios');

const app = express();

app.get('/api/users', async (req, res) => {
  const users = await axios.get('https://example.com/api/users');
  res.json(users.data);
});
```

**16. Using Axios with Node.js Stream:**

```js
axios.get('https://example.com/api/users', { responseType: 'stream' })
  .then(response => {
    response.data.pipe(fs.createWriteStream('users.json'));
  });
```

**17. Uploading a File:**

```js
const formData = new FormData();
formData.append('file', fs.createReadStream('file.txt'));

axios.post('https://example.com/api/upload', formData);
```

**18. Using Axios with Puppeteer:**

```js
const puppeteer = require('puppeteer');
const axios = require('axios');

const browser = await puppeteer.launch();
const page = await browser.newPage();
const response = await page.evaluate(() => axios.get('https://example.com/api/users'));
console.log(response.data);
```

**19. Using Axios with Cheerio:**

```js
const axios = require('axios');
const cheerio = require('cheerio');

axios.get('https://example.com')
  .then(response => {
    const $ = cheerio.load(response.data);
    console.log($('title').text());
  });
```

**20. Using Axios with React.js:**

```js
import axios from 'axios';
import React, { useState, useEffect } from 'react';

const App = () => {
  const [users, setUsers] = useState([]);

  useEffect(() => {
    axios.get('https://example.com/api/users')
      .then(response => setUsers(response.data));
  }, []);

  return (
    <div>
      {users.map(user => <li key={user.id}>{user.name}</li>)}
    </div>
  );
};
```

**21. Using Axios with Vue.js:**

```js
import axios from 'axios';
import Vue from 'vue';

new Vue({
  el: '#app',
  data: {
    users: []
  },
  mounted() {
    axios.get('https://example.com/api/users')
      .then(response => this.users = response.data);
  }
});
```

**22. Using Axios with Angular:**

```js
import { Injectable } from '@angular/core';
import { HttpClient } from '@angular/common/http';

@Injectable({
  providedIn: 'root'
})
export class UserService {

  constructor(private http: HttpClient) { }

  getUsers() {
    return this.http.get('https://example.com/api/users');
  }
}
```

**23. Using Axios with Node.js CLI:**

```js
const axios = require('axios');
const chalk = require('chalk');

axios.get('https://example.com')
  .then(response => console.log(chalk.green('Success: ', response.data)))
  .catch(error => console.log(chalk.red('Error: ', error)));
```

**24. Using Axios with TypeScript:**

```typescript
import axios from 'axios';

const getUsers = async () => {
  const response = await axios.get('https://example.com/api/users');
  return response.data;
};
```

**25. Using Axios with Fastify:**

```js
const fastify = require('fastify')({ logger: true });
const axios = require('axios');

fastify.get('/api/users', async (request, reply) => {
  const users = await axios.get('https://example.com/api/users');
  reply.send(users.data);
});
```
