Which HTTP response status codes result in then() and which in catch() ?
Last Updated :
26 Apr, 2025
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
State the core components of an HTTP response ?
Have you ever thought about how the front-end of an application communicates with the backend to get data or perform certain operations? It is done through API Requests. API stands for Application Programming Interface. The communication between our client and the API is achieved using HTTP Request
4 min read
How to get Response Status Code with Selenium WebDriver?
In web automation and testing, obtaining the HTTP response status code is crucial for validating the accessibility and health of web pages. While Selenium WebDriver is widely recognized for its powerful capabilities in automating browser interactions, it does not directly provide methods for fetchin
4 min read
How to access Previous Promise Results in Then Chain ?
In this article, we will discuss how to access previous promise results by using "then" in JavaScript. In a series of promises connected by then, each subsequent step receives the outcome of the previous promise's resolution. This sequential passing of results permits ongoing access and handling of
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. Information
4 min read
Handling Promise rejection with catch while using await
In this article, we will try to understand how we may handle Promise's rejection with a try/catch block while using await inside an asynchronous (having prefix as an async keyword) function (or a method). Syntax: Let us first quickly visualize the following illustrated syntax which we may use to cre
2 min read
What are the different stages and processes in AJAX ready states ?
In this article, we will see the different stages & processes involved in AJAX-ready states, along with knowing their importance & basic usage through the illustrations. Asynchronous JavaScript and XML(AJAX) can be used to communicate with the server without reloading the web page, so it hel
6 min read
How to Get a Response Body When Testing the Status Code in WebFlux WebClient?
Spring WebFlux is an HTTP request client. Reactor is the foundation of WebClient's functional and fluid API, allowing declarative building of asynchronous logic without requiring knowledge of threads or concurrency. It uses the same codecs that are also used to encode and decode request and response
4 min read
How to Change Status of JsonResponse in Django
When developing APIs or AJAX-based web applications in Django, one of the most common tasks is returning JSON data as a response. Django provides the JsonResponse class to send JSON data easily. However, sometimes we might need to return a custom HTTP status code along with the JSON data.JsonRespons
5 min read
Assertions in Postman and How to Use that in Scripting Window
Assertions are checks or validations we can include in our API requests to ensure that the response from the server meets certain criteria. These criteria could be based on the response's status code, headers, body content, or any other aspect of the response. Postman provides a variety of built-in
6 min read
How To Return the Response From an Asynchronous Call in JavaScript?
To return the response from an asynchronous call in JavaScript, it's important to understand how JavaScript handles asynchronous operations like fetching data, reading files, or executing time-based actions. JavaScript is a single-threaded nature means it can only handle one task at a time, but it u
3 min read