The Wayback Machine - https://web.archive.org/web/20240720003019/https://www.geeksforgeeks.org/which-http-response-status-codes-result-in-then-and-which-in-catch/
Open In App

Which HTTP response status codes result in then() and which in catch() ?

Last Updated : 19 Dec, 2022
Improve
Suggest changes
Post a comment
Like Article
Like
Save
Share
Report

Axios is a popular JavaScript library for making HTTP requests. It uses promises to handle the response from the server, and provides a convenient then() and catch() syntax for working with the data or handling errors.

Have you ever wondered which HTTP response status codes will cause axios to call the then() callback, and which will cause it to call the catch() callback? In this article, we’ll explain how axios handles HTTP response status codes and show you how to handle different types of responses in your code.

1. Understanding HTTP response status codes

HTTP is the protocol that powers the web. When a client (such as a web browser) makes a request to a server, the server responds with a status code indicating the outcome of the request. Status codes are divided into five categories, based on their first digit:

  • 1xx: Informational
  • 2xx: Success
  • 3xx: Redirection
  • 4xx: Client errors
  • 5xx: Server errors

The most common status codes you’ll encounter are in the 2xx and 4xx/5xx ranges. 2xx codes indicate that the request was successful, while 4xx/5xx codes indicate that there was an error.

Here are some examples of common 2xx and 4xx/5xx status codes:

  • 200: OK
  • 201: Created
  • 204: No Content
  • 400: Bad Request
  • 404: Not Found
  • 500: Internal Server Error

2. How axios handles HTTP response status codes

When you make an HTTP request with axios, the library returns a promise. If the request is successful (i.e. the server responds with a 2xx status code), the promise will be resolved and the then() callback will be called with the response data. On the other hand, if the request fails (i.e. the server responds with a 4xx or 5xx status code), the promise will be rejected and the catch() callback will be called with the error.

Example: In this example, if the HTTP request to /some/url receives a response with a 2xx status code, the success handler in the then() callback will be called with the response data. On the other hand, if the response has a 4xx or 5xx status code, the error handler in the catch() callback will be called with the error.

Javascript




axios.get('/some/url')
    .then(response => {
        // handle success
    })
    .catch(error => {
        // handle error
    });


3. Handling different types of responses

Now that you know how axios handles HTTP response status codes, you can write code that can handle different types of responses from the server. For example, you might want to do something different depending on whether the request was successful or not.

Example: This example of how one could handle a successful (2xx) and a failed (4xx/5xx) response differently.

Javascript




axios.get('/some/url')
    .then(response => {
        // handle success
        if (response.status === 200) {
            // do something with the data...
        } else if (response.status === 201) {
            // do something else...
        }
    })
    .catch(error => {
        // handle error
        if (error.response.status === 400) {
            // handle bad request error...
        } else if (error.response.status === 404) {
            // handle not found error...
        }
    });


In this example, we’re using the `status` property of the response object to check the HTTP status code. If the code is 2xx, we handle the success case in the `then()` callback. If the code is 4xx/5xx, we handle the error case in the `catch()` callback.

You can use this pattern to handle any type of HTTP response and write more robust code that can handle different scenarios.

Conclusion: In this article, we learnt how axios handles HTTP response status codes and the write code that can handle different types of responses. We covered the basic categories of HTTP status codes, and showed you how to use the `then()` and `catch()` callbacks of an axios promise to handle successful and failed responses.



Similar Reads

Difference Between try..catch and module.then().catch()?
In JavaScript, "try..catch" and "module.then().catch()" are used for error handling. "try..catch" handles errors in synchronous code, providing a way to catch and manage exceptions locally. On the other hand, "module.then().catch()" handles errors in asynchronous code, particularly with Promises, allowing for the management of errors that occur dur
3 min read
HTTP status codes | Successful Responses
The HTTP status codes are used to indicate that any specific HTTP request has successfully completed or not. The HTTP status codes are categorized into five sections those are listed below: Informational responses (100–199) Successful responses (200–299) Redirects (300–399) Client errors (400–499) Server errors (500–599) There are ten Successful Re
4 min read
HTTP status codes | Informational Responses
The HTTP status codes are used to indicate that any specific HTTP request has successfully completed or not. The HTTP status codes are categorized into five sections those are listed below: Informational responses (100–199) Successful responses (200–299) Redirects (300–399) Client errors (400–499) Server errors (500–599) There are four Informationa
2 min read
HTTP status codes | Redirection Responses
HTTP status codes are a conversation between your browser and the site server. The server gives responses to the browser's request in the form of a three-digit code known as HTTP status codes. The HTTP status codes are categorized into five sections which are listed below. Informational responses (100–199) Successful responses (200–299) Redirects (
2 min read
HTTP status codes | Server Error Responses
HTTP status codes is a conversation between your browser and the site server. The server gives responses to the browser's request in the form of a three-digit code known as HTTP status codes. Categories of HTTP status codes are. Informational responses (100–199) Successful responses (200–299) Redirects (300–399) Client errors (400–499) Server error
3 min read
HTTP status codes | Client Error Responses
The browser and the site server have a conversation in the form of HTTP status codes. The server gives responses to the browser’s request in the form of a three-digit code known as HTTP status codes. The categorization of HTTP status codes is done in five sections which are listed below. Informational responses (100–199)Successful responses (200–29
4 min read
10 Most Common HTTP Status Codes
You search for anything on Google, and by this, a request is being sent to the server, then the server responds. This response is done using HTTP (HyperText Transfer Protocol) by the server. There can be two cases - the server may respond with the information it has, or else it may show an error with a code. Whenever there's an error in the back en
5 min read
What do you understand by the HTTP Status Codes ?
The HTTP or the HyperText Transfer Protocol is a protocol of the application layer. It helps in establishing communication between a web browser and a web server. When a client requests any information, the browser sends a response using numeric status codes. These status codes are in the form of 3-digit numbers ranging from 100 - 599. The client t
3 min read
What are HTTP Status Codes ?
HTTP Protocol is used everywhere from the server page to each service communication, deploying service to monitoring service. HTTP codes give an extension of HTTP protocol to debug network problems using curl, telnet able to check server availability, service responses HTTP status code is used for search engine optimization of how your pages get in
8 min read
How to intercept response.send() / response.json() in Express JS
In the context of Express , "intercept" usually refers to the process of capturing or modifying a request or response in a middleware function before it reaches its final destination (e.g., a route handler) or before it is sent back to the client. In Express, intercepting response.send() or response.json() can be achieved by using middleware functi
3 min read
three90RightbarBannerImg