frigus02/RESTer

How this tool gives Response in red or in green?

Closed this issue · 3 comments

I'm a new hand studying http-server recently, I know that it returns different HTTP Status Code such as 200 and 404 according to different cases, but I think in programs they are the same so I want to know how does your tool distinguish them?

Hi @Ingsuifon, you're right. The HTTP status code is just a number returned by the server as part of the response.

The code in RESTer is a bit convoluted. It uses a few browser extension APIs to get around security limitations of normal websites. This makes it quite hard to follow. But essentially, RESTer just uses fetch() to make an HTTP request and then reads the status from the response.

const fetchResponse = await fetch(request.url, init);

This results in a number, which would be in the rage of 100 - 599. By convention status codes are divided into 5 groups. I decided to show this in RESTer by applying a different color per group:

_computeResponseBadgeType(status) {
if (status >= 100 && status < 200) {
return 'info';
} else if (status >= 200 && status < 300) {
return 'success';
} else if (status >= 300 && status < 400) {
return 'warn';
} else if (status >= 400) {
return 'error';
}
}

Does this make sense? Did I understand your question correctly?

Thank you very much. I haven't studied javaScript so I temporarily cann't read your code. But I understand your design concept. Thank you.

Great. I'm going to close this issue. But please feel free to re-open or open a new one if you have further questions.