Option to specify headers for RESTful components (or sensor at least)

I just had to modify the RESTtful sensor, one of the API’s I tried to use required a user-agent to be specified, but the current RESTful sensor does not provide a user-agent. Supplying headers={‘User-agent’: ‘Home Assistant’} to the request was enough to make it work in my altered version.

But maybe we need to set a default user-agent or expose the headers so we can supply them ourselves in case other RESTful services need other headers to be present ?

It was this API that required it: https://haveibeenpwned.com/api/v2 i’m currently using it in a RESTful sensor to display the number of breaches my email accounts have had and i’m gonna add automations to notify when the number changes.

i used it like this :

sensor:
  - platform: rest
    resource: https://haveibeenpwned.com/api/v2/breachedaccount/putyouremailhere
    name: putyouremailhere Breaches
    value_template: '{{ value_json | length }}'
    scan_interval: 3600

There is another problem, this service only allows X requests per X seconds. If you specify more then one sensor like this (for multiple email addresses) you have no control on HASS startup over this. A delay option might solves this. Currently I specified different scan_interval's for each of my email accounts so the problem only happens mostly when Home Assistant starts and due to difference in scan_intervals there is less potential that 2 RSTful sensors do the request against the same service quickly in succession (so that one of the requests fails).

Not sure if a delay option is feasible though.

i could not get it to work so i’m making a seperate component for this now where you just supply a list of email addresses in the configuration. However i do still think that the headers should be exposed and configureable from the configuration

Auth and header are not supported at the moment. Let me see what I can do…

I don’t really need it anymore myselve as i’m doing my own requests now in the email breaches component but i guess sooner or later somebody is goana need it :slight_smile:

for reference, in case anyone wanted to use the “have i been pwned” service as well. I create a seperate component for it where you can just specify your email accounts in your configuration

Are headers also supported for the notify.rest platform?

It would be great if headers would be supported for switch.rest and/or rest_command.
For use case, see Rest Post request to endpoint from HASS - Help with Syntax?

Would it make sense to extract the common rest logic, such as headers support, from switch.rest, sensor.rest, rest_command, etc. into a common file?

1 Like

Is there any progress in adding headers to rest components? Especially, rest_command and rest.notify?

1 Like

+1

I would like to see support for headers added to the RESTful switch so I can use it on the Portainer API.

And I would also like to see support for templates in the headers!

Use case: Bearer authentication with tokens that expire after 8 hours (Portainer API).

+1 for this - headers are needed for authentication for many APIs

I think this feature request could be marked as resolved/implemented. I follow the Home Assistant repository on GitHub and support for headers was recently added to the RESTful command and switch. The docs for the RESTful command reflect this, but the docs for the RESTful switch seem to need updating still.

The RESTful sensor and binary sensor received support for headers back in September/October 2016. The only RESTful component missing out belongs to the notify platform.

Starting with the next major release, the RESTful notify component should also support headers. See PR @ https://github.com/home-assistant/home-assistant/pull/13674

I believe that will complete this feature request.

is it also possible to have dynamc headers? (templating) ?