Override Content-Type for GET endpoint i.e. force xmltojson conversion in rest sensor

ref. override Content-Type for GET endpoint i.e. force xmltojson conversion in rest sensor · Issue #108292 · home-assistant/core · GitHub

“When an IoT device does not return any Content-Type then the XML data endpoint does not get converted to JSON in a RESTful templated sensor”

I saw that they said it should be a feature request, but I disagree. the resource you are requesting ends in .xml - it’s not exactly a leap to assume that the received content is xml in the absence of a content-type header.

The workaround by the way, is to use NodeRed, setup a HTTP In endpoint, request the resource with a HTTP Request node, add the “content-type: text/xml” header to the msg object. And finally return it to the HTTP Out node. Then change your rest sensor resource to point at the NodeRed resource instead.

For a human, sure, but a machine relies entirely on content-type / mimetype. There is no notion of “file extension” in HTTP / REST.

Nice tip about NodeRed, I will dive into that rabbit-hole of learning :grin:

Yeah I think this is pretty clearly an enhancement though, but a really obvious one that should be added unless the expectation is to NodeRed all the things? From a user experience point-of-view there’s already an expectation to be able to specify the Content-Type for outgoing requests, and a dependency on Content-Type (as opposed to file extension suffix as you rightly point out). It would cleanly override the processing of results without too much added confusion and it’s sticking with existing features to expose them without feature creep.

With some more discussion about how that should be realized…

1 Like

and for anyone that needs a visual:

After deploying it ends up being something like:

$ curl --head http://homeassistant.lan:1880/endpoint/CONFIG.XML
HTTP/1.1 200 OK
Server: nginx
Date: Thu, 18 Jan 2024 13:15:44 GMT
Content-Type: application/xml; charset=utf-8
Content-Length: 15339
Connection: keep-alive
Access-Control-Allow-Origin: *
ETag: W/"3beb-d6JitAAZl5Ir8xNoRUBd2nt0SdY"
Vary: Accept-Encoding
X-Content-Type-Options: nosniff
X-XSS-Protection: 1; mode=block
X-Robots-Tag: none
Cache-Control: no-store, no-cache, must-revalidate, proxy-revalidate, max-age=0

Which is mercifully helpful because I’ve never touched NodeRed before then. Thanks. Also, I still consider this a bug / needs enhancement in Home Assistant.

1 Like