1import React from 'react';
2
3import axios from 'axios';
4
5export default class PersonList extends React.Component {
6 state = {
7 persons: []
8 }
9
10 componentDidMount() {
11 axios.get(`https://jsonplaceholder.typicode.com/users`)
12 .then(res => {
13 const persons = res.data;
14 this.setState({ persons });
15 })
16 }
17
18 render() {
19 return (
20 <ul>
21 { this.state.persons.map(person => <li>{person.name}</li>)}
22 </ul>
23 )
24 }
25}
26
1// Send a POST request
2axios({
3 method: 'post',
4 url: '/user/12345',
5 data: {
6 firstName: 'Fred',
7 lastName: 'Flintstone'
8 }
9});
1const req = async () => {
2 const response = await axios.get('https://dog.ceo/api/breeds/list/all')
3 console.log(response)
4}
5req() // Calling this will make a get request and log the response.
1// GET request for remote image
2axios({
3 method: 'get',
4 url: 'http://bit.ly/2mTM3nY',
5 responseType: 'stream'
6})
7 .then(function(response) {
8 response.data.pipe(fs.createWriteStream('ada_lovelace.jpg'))
9});