本文介绍了Flask Api 解析 xml post 请求返回错误的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在编写一个 python-flask api 来解析和操作 xml,使用 xml to dict.以下是我的初始代码:

I am writing a python-flask api to parse and manipulate xml using xml to dict. Below is my initial code:

from flask import Flask, jsonify, abort, make_response, request,
render_template
import sys, sqlite3, requests, datetime, time, re, json, optparse
import xmltodict
import helpers
import platform
app = Flask(__name__)

start = int(round(time.time()))
print("api running")

@app.route("/", methods=['GET', 'POST'])
def parse_xml():
    content_dict = xmltodict.parse(request.data)
    return content_dict

if __name__ == '__main__':
    print("Starting python app")
    app.run(host='0.0.0.0', port=8080, debug=False)

以下 curl 请求:curl -H "Authorization: Bearer Token" -X POST -d ''网址

The following curl request: curl -H "Authorization: Bearer Token" -X POST -d '<xml><From>Jack</From><Body>Hello, it worked!</Body></xml>' url

返回错误:

2018-04-18T13:33:38.831293275Z [2018-04-18 13:33:38,830] 应用程序错误:/[POST] 2018-04-18T13:33:38.831324305Z 上的异常回溯(大多数最近通话最后一次):2018-04-18T13:33:38.831331225Z 文件/usr/lib/python2.7/site-packages/flask/app.py",第 1982 行,在wsgi_app 2018-04-18T13:33:38.831336961Z 响应 =self.full_dispatch_request() 2018-04-18T13:33:38.83134283Z 文件/usr/lib/python2.7/site-packages/flask/app.py",第 1614 行,在full_dispatch_request 2018-04-18T13:33:38.831352063Z rv =self.handle_user_exception(e) 2018-04-18T13:33:38.831357531Z 文件/usr/lib/python2.7/site-packages/flask/app.py",第 1517 行,在handle_user_exception 2018-04-18T13:33:38.83136302Z
reraise(exc_type, exc_value, tb) 2018-04-18T13:33:38.831367481Z 文件/usr/lib/python2.7/site-packages/flask/app.py",第 1612 行,在full_dispatch_request 2018-04-18T13:33:38.831372038Z rv =self.dispatch_request() 2018-04-18T13:33:38.831377673Z 文件/usr/lib/python2.7/site-packages/flask/app.py",第 1598 行,在dispatch_request 2018-04-18T13:33:38.831385927Z 返回self.view_functionsrule.endpoint2018-04-18T13:33:38.831391913Z 文件/src/simpleapp.py",第 14 行,在parse_xml 2018-04-18T13:33:38.831396571Z content_dict =xmltodict.parse(request.data) 2018-04-18T13:33:38.831401027Z 文件/usr/lib/python2.7/site-packages/xmltodict.py",第 330 行,解析中2018-04-18T13:33:38.831407166Z parser.Parse(xml_input, True)2018-04-18T13:33:38.831412259Z ExpatError:未找到元素:第 1 行,第 0 列 2018-04-18T13:33:38.831705887Z 100.96.2.171 - - [18/Apr/201813:33:38]POST/HTTP/1.1"500 -

任何帮助将不胜感激,谢谢.

Any help would be appreciated, thank you.

推荐答案

您必须将 curl 请求更改为 key=value 对以便于处理.

You have to change you're curl request to be key=value pairs for easy handling.

卷曲:

 curl -H "Authorization: Bearer Token" -X POST -d 'SomeKey=<xml><From>Jack</From><Body>Hello, it worked!</Body></xml>' http://127.0.0.1:5000

蟒蛇:

from flask import Flask, jsonify, request
import xmltodict

app = Flask(__name__)

@app.route("/", methods=['GET', 'POST'])
def parse_xml():
    xml_data = request.form['SomeKey']
    content_dict = xmltodict.parse(xml_data)
    return jsonify(content_dict)

回复:

{
  "xml": {
      "Body": "Hello, it worked!",
      "From": "Jack"
  }
}

这篇关于Flask Api 解析 xml post 请求返回错误的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

08-22 20:01