How to Fix: How can I get the status code from an HTTP error in Axios?
Get error data from Axios HTTP errors in JSON format.
📋 Table of Contents
To get the status code from an HTTP error in Axios, you can use the `config` property of the error object. The `config` property contains the original options passed to the request, which includes the URL and any headers.
💡 How to Get Status Code
- Use the `config` property of the error object:
Example:
axios.get('foo.example', { headers: { 'Content-Type': 'application/json' } }).then((response) => {}).catch((error) => { console.log(error.config); });This will log an object with the status code, reason phrase, and response data.
🚀 How to Get Status Code
- Use the `response.config` property:
Example:
axios.get('foo.example', { headers: { 'Content-Type': 'application/json' } }).then((response) => {}).catch((error) => { console.log(response.config); });This will log an object with the status code, reason phrase, and response data.
🎯 Final Words
By using the `config` or `response.config` property, you can easily access the status code from an HTTP error in Axios.
❓ Frequently Asked Questions
🛠️ Related Fixes
How to Fix: Stuck in tutorial hell after 4 years: How do I b
Learn to build websites and think independently with coding skills.
How to Fix: Trying to sync mutliple audio tracks to a movie
Complex audio track synchronization can be challenging due to the larg
How to Fix: Failed to merge latest branches from upstream re
Update local repository with latest upstream branches.