1const axios = require('axios');
2
3// httpbin.org gives you the headers in the response
4// body `res.data`.
5// See: https://httpbin.org/#/HTTP_Methods/get_get
6const res = await axios.get('https://httpbin.org/get', {
7 headers: {
8 'Test-Header': 'test-value'
9 }
10});
11
12res.data.headers['Test-Header']; // "test-value"
1// Make a request for a user with a given ID
2axios.get('/user?ID=12345')
3 .then(function (response) {
4 console.log(response);
5 })
6 .catch(function (error) {
7 console.log(error);
8 });
9
10// Optionally the request above could also be done as
11axios.get('/user', {
12 params: {
13 ID: 12345
14 }
15 })
16 .then(function (response) {
17 console.log(response);
18 })
19 .catch(function (error) {
20 console.log(error);
21 });
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});