【问题标题】:Flask : Processes the XML in GET methodFlask:在 GET 方法中处理 XML
【发布时间】:2017-04-03 12:32:05
【问题描述】:

我正在尝试使用 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

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

【问题讨论】:

    标签: python xml rest flask flask-restful


    【解决方案1】:

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

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

    【讨论】:

      猜你喜欢
      • 2017-06-20
      • 1970-01-01
      • 1970-01-01
      • 2017-04-20
      • 1970-01-01
      • 2018-06-04
      • 2015-09-13
      • 2021-12-18
      • 1970-01-01
      相关资源
      最近更新 更多