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 "C:\Python27\lib\xml\etree\ElementTree.py", line 647, in parse
source = open(source, "rb")
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
请帮助我了解我哪里出错了。提前致谢。
我正在尝试使用 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 "C:\Python27\lib\xml\etree\ElementTree.py", line 647, in parse
source = open(source, "rb")
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
请帮助我了解我哪里出错了。提前致谢。