What does HTTP status code mean by dividing it by 100 and comparing it to 2?

Asked 2 years ago, Updated 2 years ago, 97 views

An OSS has verified the HTTP status code as follows:

private void checkHttpCode(){
  if(http.statusLine.Code/100!=2){
    through new Exception (http.statusLine.code, http.statusLine.reason);
  }
}

Please let me know if there are any reasons or benefits to verify with these conditional expressions.

http d

2022-09-29 22:30

1 Answers

If an HTTP request is received, a 200 Series status code is returned.
In addition to the well-known OK 200, there are 201-208 and 226.

HTTP Status Code (Wikipedia)

In D language, as in C language, an integer divided by an integer is an integer.
In other words, 200/100 is 2, 201/100 is 2, 208/100 is 2, and 226/100 is 2.

So,

private void checkHttpCode(){
  if (http.statusLine.Code != 200 && http.statusLine.Code != 201 && http.statusLine.Code != 202 && http.statusLine.Code != 203 && http.statusLine.Code != 204 && http.statusLine.Code != 205 && http.statusLine.Code != 206 && http.statusLine.Code != 207 && http.statusLine.Code != 208 && http.statusLine.Code != 226) {
    through new Exception (http.statusLine.code, http.statusLine.reason);
  }
}

If you know how to divide integers and integers, you can write the long code as simple as the code in the question.

The code in question has the advantage of being "short" and "easy to understand" compared to the code above.

A status code that is not in the 200s means that the request was not received (something went wrong).
In such cases, it is very common sense code to make exceptions (throw the exceptions and move on to exception handling).


2022-09-29 22:30

If you have any answers or tips


© 2024 OneMinuteCode. All rights reserved.