How to detect invalid server responses using REST API calls to create sensors?

If the server response is 200 OK then everything is OK, but what if it is 401 INVALID or any other invalid message? How will I know? The API call is part of a YAML config like this:

sensor:
  - platform: rest
    resource: https://<some valid remote API URL>
    method: GET
    headers: 
      Authorization: <my secret authorization token>
    value_template: '{{ <some valid JSON attribute> }}'
    name: Name-Of-Sensor

Try something like this:

value_template: '{{ <some valid JSON attribute> if <some valid JSON attribute> else 0 }}'

That’s not a good reply. The status code is not returned in a JSON attribute. That’s why I’m asking.
401 errors returns an HTML response while i.e. a 406 returns a JSON response. There’s no way to know that in advance except by analysing the status code.
So how can I fetch the status code?