I want to get the status code from the HTTP header.

Asked 2 months ago, Updated 2 months ago, 2 views

Hello
Let me ask you a question.
Is it possible to get the status code from the HTTP header?
The language is javascript.

javascript

2022-09-30 14:23

1 Answers

For example, to send a request to an API called https://jsonplaceholder.typicode.com/users/1, get its HTTP response status code, and then output it to the debugger web console, you can use the Fetch API as follows:Now, use the Response.status property to retrieve the HTTP response status code.[1].

[1]
 fetch('https://jsonplaceholder.typicode.com/users/1')
  .then(response=>console.log(response.status)));//=>200

fetch('https://jsonplaceholder.typicode.com/users/100')
  .then(response=>console.log(response.status)));//=>404

 document.querySelector("button").addEventListener("click",()=>{
  fetch('https://jsonplaceholder.typicode.com/users/1')
    .then(response=>console.log(response.status)));//=>200

  fetch('https://jsonplaceholder.typicode.com/users/100')
    .then(response=>console.log(response.status)));//=>404
});
<button>fetch>/button>


2022-09-30 14:23

If you have any answers or tips


© 2022 OneMinuteCode. All rights reserved.