1

There's an API that I am experimenting with that outputs XML.

Could anyone tell me if I have something wrong my code to parse the API response or is it a problem with the API itself?

I can use a put command with this:

import requests
import xml
from xml.etree import ElementTree

response = requests.post("https://openadrtester.herokuapp.com/api/v1/cpp/senorio/three")
string_xml = response.content
tree = xml.etree.ElementTree.fromstring(string_xml)

xml.etree.ElementTree.dump(tree)

But it will error out on:

Traceback (most recent call last):
  File "C:\Users\bbartling\OneDrive - Slipstream\Desktop\myAPI\tut\test.py", line 7, in <module>
    tree = xml.etree.ElementTree.fromstring(string_xml)
  File "C:\Users\bbartling\AppData\Local\Programs\Python\Python37\lib\xml\etree\ElementTree.py", line 1315, in XML
    parser.feed(text)
  File "<string>", line None
xml.etree.ElementTree.ParseError: not well-formed (invalid token): line 1, column 0

XML is also viewable through the browser on a get request:

https://openadrtester.herokuapp.com/

3
  • What does string_xml contain if you print() it in your code? Commented Oct 27, 2020 at 19:01
  • b'{"notification": "True", "startTime": "2:00PM", "duration": "6 Hours", "randomization": "None", "rampUp": "None", "recovery": "None", "numberOfSignals": "2", "signalNameSimple": [{"signalType": "level", "units": "None", "numberOfIntervals": "None", "intervalDuration": "None", "typicalIntervalValues": "1,2,1", "signalTarget": "None"}], "signalNameElectricityPrice": [{"signalType": "price", "units": "USD per kWh", "numberOfIntervals": "3", "intervalDuration": "1 hour,4 hour,1 hour", "typicalIntervalValues": "$0.50,$0.75,$0.50", "signalTarget": "None"}]}\n' Commented Oct 27, 2020 at 19:01
  • That's json, not XML. Commented Oct 27, 2020 at 19:02

1 Answer 1

2

The data is not XML, but Json (parse it with .json() method, then access like normal python structure):

import json
import requests

response = requests.post("https://openadrtester.herokuapp.com/api/v1/cpp/senorio/three")
data = response.json()

print(json.dumps(data, indent=4))

Prints:

{
    "notification": "True",
    "startTime": "2:00PM",
    "duration": "6 Hours",
    "randomization": "None",
    "rampUp": "None",
    "recovery": "None",
    "numberOfSignals": "2",
    "signalNameSimple": [
        {
            "signalType": "level",
            "units": "None",
            "numberOfIntervals": "None",
            "intervalDuration": "None",
            "typicalIntervalValues": "1,2,1",
            "signalTarget": "None"
        }
    ],
    "signalNameElectricityPrice": [
        {
            "signalType": "price",
            "units": "USD per kWh",
            "numberOfIntervals": "3",
            "intervalDuration": "1 hour,4 hour,1 hour",
            "typicalIntervalValues": "$0.50,$0.75,$0.50",
            "signalTarget": "None"
        }
    ]
}

EDIT: To modify request header to obtain XML:

import requests

response = requests.post("https://openadrtester.herokuapp.com/api/v1/cpp/senorio/three", headers={'Accept': 'application/xml'})
print(response.text)

Prints:

<?xml version="1.0" ?><response><notification>True</notification><startTime>2:00PM</startTime><duration>6 Hours</duration><randomization>None</randomization><rampUp>None</rampUp><recovery>None</recovery><numberOfSignals>2</numberOfSignals><signalNameSimple><signalType>level</signalType><units>None</units><numberOfIntervals>None</numberOfIntervals><intervalDuration>None</intervalDuration><typicalIntervalValues>1,2,1</typicalIntervalValues><signalTarget>None</signalTarget></signalNameSimple><signalNameElectricityPrice><signalType>price</signalType><units>USD per kWh</units><numberOfIntervals>3</numberOfIntervals><intervalDuration>1 hour,4 hour,1 hour</intervalDuration><typicalIntervalValues>$0.50,$0.75,$0.50</typicalIntervalValues><signalTarget>None</signalTarget></signalNameElectricityPrice></response>
Sign up to request clarification or add additional context in comments.

3 Comments

When people use like a curl -i -H "Accept: application/xml" is the -H a header arg?
If so Is it easy to modify my requests script for that?
Would there be any chance you could help me out with this one? :) stackoverflow.com/questions/64573932/…

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.