How can I get the status code from an http error in Axios?

This may seem stupid, but I’m trying to get the error data when a request fails in Axios.

axios.get('foo.com')
    .then((response) => {})
    .catch((error) => {
        console.log(error) //Logs a string: Error: Request failed with status code 404
    })

Instead of the string, is it possible to get an object with perhaps the status code and content? For example:

Object = {status: 404, reason: 'Not found', body: '404 Not found'}

88 thoughts on “How can I get the status code from an http error in Axios?”

  1. Hello there! I know this is kinda off topic however ,
    I’d figured I’d ask. Would you be interested in exchanging
    links or maybe guest authoring a blog post or vice-versa?
    My site goes over a lot of the same topics as yours and I feel we could greatly benefit from each other.
    If you’re interested feel free to send me an email.

    I look forward to hearing from you! Fantastic blog by the way! https://www.herpessymptomsinmen.org/where-to-buy-hydroxychloroquine/

    Reply

Leave a Comment