Forked from sabljakovich/log-request-and-response-axios-interceptor.js
Created
May 14, 2021 09:35
-
-
Save einnar82/82d9b0e601d6748ec2435ff21a20527e to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
const axios = require('axios').default; | |
axios.interceptors.request.use(x => { | |
const headers = { | |
...x.headers.common, | |
...x.headers[x.method], | |
...x.headers | |
}; | |
['common','get', 'post', 'head', 'put', 'patch', 'delete'].forEach(header => { | |
delete headers[header] | |
}) | |
const printable = `${new Date()} | Request: ${x.method.toUpperCase()} | ${x.url} | ${ JSON.stringify( x.data) } | ${ JSON.stringify(headers)}` | |
console.log(printable) | |
return x; | |
}) | |
axios.interceptors.response.use(x => { | |
const printable = `${new Date()} | Response: ${x.status} | ${ JSON.stringify(x.data) }` | |
console.log(printable) | |
return x; | |
}) | |
axios.post('https://jsonplaceholder.typicode.com/posts', { | |
myProperty: true | |
}).then() |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment