Hi,
I’m trying to call a REST api with the REST platform. It’s a SOAP POST call and I get a warning in the logg that it can’t parse the response to JSON. The idea is to get the exchange rate between Euro and SEK. I have tried many ways but this is the current setup:
In configuration.yaml
- platform: rest
name: EURTOSEK
resource: http://swea.riksbank.se/sweaWS/services/SweaWebServiceHttpSoap12Endpoint
method: POST
payload: '<soap:Envelope xmlns:soap="http://www.w3.org/2003/05/soap-envelope" xmlns:xsd="http://swea.riksbank.se/xsd"><soap:Header/><soap:Body><xsd:getLatestInterestAndExchangeRates><languageid>sv</languageid><seriesid>SEKEURPMI</seriesid></xsd:getLatestInterestAndExchangeRates></soap:Body></soap:Envelope>'
headers:
Content-Type: application/soap+xml
charset: UTF-8
action: urn:getLatestInterestAndExchangeRates
json_attributes_path: "$.body.getLatestInterestAndExchangeRatesResponse"
json_attributes:
- return
value_template: '{{ value_json[0] }}'
Result in log:
2021-02-10 10:49:34 DEBUG (MainThread) [homeassistant.components.rest.data] Updating from http://swea.riksbank.se/sweaWS/services/SweaWebServiceHttpSoap12Endpoint
2021-02-10 10:49:34 DEBUG (MainThread) [httpx._client] HTTP Request: POST http://swea.riksbank.se/sweaWS/services/SweaWebServiceHttpSoap12Endpoint "HTTP/1.1 200 OK"
2021-02-10 10:49:34 DEBUG (MainThread) [homeassistant.components.rest.sensor] Data fetched from resource: <?xml version="1.0" encoding="UTF-8"?><SOAP-ENV:Envelope xmlns:SOAP-ENV="http://www.w3.org/2003/05/soap-envelope"><SOAP-ENV:Body><ns0:getLatestInterestAndExchangeRatesResponse xmlns:ns0="http://swea.riksbank.se/xsd"><return xmlns=""><groups xmlns=""><groupid xmlns="">130</groupid><groupname xmlns="">Valutor mot svenska kronor</groupname><series xmlns=""><seriesid xmlns="">SEKEURPMI</seriesid><seriesname xmlns="">1 EUR</seriesname><unit xmlns="">1.0E0</unit><resultrows xmlns=""><date xmlns="">2021-02-09</date><value xmlns="">1.01162E1</value></resultrows></series></groups></return></ns0:getLatestInterestAndExchangeRatesResponse></SOAP-ENV:Body></SOAP-ENV:Envelope>
2021-02-10 10:49:34 WARNING (MainThread) [homeassistant.components.rest.sensor] REST result could not be parsed as JSON
2021-02-10 10:49:34 DEBUG (MainThread) [homeassistant.components.rest.sensor] Erroneous JSON: <?xml version="1.0" encoding="UTF-8"?><SOAP-ENV:Envelope xmlns:SOAP-ENV="http://www.w3.org/2003/05/soap-envelope"><SOAP-ENV:Body><ns0:getLatestInterestAndExchangeRatesResponse xmlns:ns0="http://swea.riksbank.se/xsd"><return xmlns=""><groups xmlns=""><groupid xmlns="">130</groupid><groupname xmlns="">Valutor mot svenska kronor</groupname><series xmlns=""><seriesid xmlns="">SEKEURPMI</seriesid><seriesname xmlns="">1 EUR</seriesname><unit xmlns="">1.0E0</unit><resultrows xmlns=""><date xmlns="">2021-02-09</date><value xmlns="">1.01162E1</value></resultrows></series></groups></return></ns0:getLatestInterestAndExchangeRatesResponse></SOAP-ENV:Body></SOAP-ENV:Envelope>
I have it all working in a Python script that looks like this:
import requests
from xml.etree import ElementTree
url = 'http://swea.riksbank.se/sweaWS/services/SweaWebServiceHttpSoap12Endpoint'
# payload = "message.xml"
payload = """
<soap:Envelope xmlns:soap="http://www.w3.org/2003/05/soap-envelope" xmlns:xsd="http://swea.riksbank.se/xsd">
<soap:Header/>
<soap:Body>
<xsd:getLatestInterestAndExchangeRates>
<languageid>sv</languageid>
<seriesid>SEKEURPMI</seriesid>
</xsd:getLatestInterestAndExchangeRates>
</soap:Body>
</soap:Envelope>"""
headers = {'Content-Type': 'application/soap+xml', 'charset': 'UTF-8', 'action': 'urn:getLatestInterestAndExchangeRates'}
response = requests.post(url, data = payload, headers = headers)
root = ElementTree.fromstring(response.content)
print(float(root[0][0][0][0][2][3][1].text))
Ideas?
Thanks!