Getting response status

Very often, we need to check the status of the request. We can do this as follows:

fetch("https://jsonplaceholder.typicode.com/posts").then(response => {
console.log(response.status, response.ok);
});
  • The response status property gives the HTTP status code of the response
  • The response ok property is a boolean and returns whether the HTTP status code is in the 200 range

If we run the previous code, we get 200 and true output to the console.

Let's try an example request where the post doesn't exist:

fetch("https://jsonplaceholder.typicode.com/posts/1001").then(response => {
console.log(response.status, response.ok);
});

If we run the preceding code, we get 404 and false output to the console.

..................Content has been hidden....................

You can't read the all page of ebook, please click here login for view all page.
Reset
3.144.28.70