2017-04-03 74 views
-1

我正在尝试使用烧瓶创建REstful Web服务。但是我在处理GET请求中的xml数据时遇到了麻烦。烧瓶:在GET方法中处理X​​ML

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 

请帮助我理解我要去的地方错了。提前致谢。

回答

0

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

想要ElementTree.fromstring()docs)。

+1

谢谢。我知道了。它的工作现在很好。 – May