Flask:在 GET 方法中处理 XML

Flask : Processes the XML in GET method

我正在尝试使用 Flask 创建 REstful Web 服务。但是我在处理 GET 请求中的 xml 数据时遇到了问题。

uri="http://127.0.0.1:5000/test/api/getfamilyinfo"

request_body='''
<StayInfo>
    <district>Khurda</district>
    <village>BBSR</village>
    <unit>Hogwarts</unit>
</StayInfo>
'''

body_format = {'Content-Type': 'application/xml'}

requests.get(uri, data = request_body, verify = False, headers = body_format)

我遇到错误:

  File &quot;C:\Python27\lib\xml\etree\ElementTree.py&quot;, line 647, in parse
source = open(source, &quot;rb&quot;)
TypeError: coercing to Unicode: need string or buffer, Response found</textarea>

我的代码:

@app.route('/test/api/getfamilyinfo', methods=['GET'])
def getfamilyinfo():
    errors = []
    results = {}

    if request.method == "GET":
        try:
            r=request.data

        except Exception,e:
            resp = jsonify({"error": str(e)})
           return resp, status.HTTP_400_BAD_REQUEST

        if r:
            eTree = ElementTree.parse(r) ## The code is breaking here

请帮助我了解我哪里出错了。提前致谢。

ElementTree.parse() (docs) 需要一个文件名(或文件对象)。

你想要 ElementTree.fromstring() (docs).