4

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

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 -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 Traceback(最近一次通话最后): 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.831352063 Z 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 return self.view_functionsrule.endpoint 2018-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/2018 13:33:38] “POST / HTTP/1.1” 500 -

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

4

2 回答 2

3

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

卷曲:

 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

Python:

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"
  }
}
于 2018-04-18T22:22:35.470 回答
2

您还可以使用 curl 来处理postXML 文件内容:

curl -X POST -d @data.xml  -H 'Accept: application/xml'  -H 'Content-Type: application/xml' url

data.xml内容:

<xml>
 <From>Jack</From>
 <Body>Hello, it worked!</Body>
</xml>
于 2018-04-18T23:04:45.850 回答