4

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)

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

Returns the errors:

2018-04-18T13:33:38.831293275Z [2018-04-18 13:33:38,830] ERROR in app: Exception on / [POST] 2018-04-18T13:33:38.831324305Z Traceback (most recent call last): 2018-04-18T13:33:38.831331225Z File "/usr/lib/python2.7/site-packages/flask/app.py", line 1982, in wsgi_app 2018-04-18T13:33:38.831336961Z response = self.full_dispatch_request() 2018-04-18T13:33:38.83134283Z File "/usr/lib/python2.7/site-packages/flask/app.py", line 1614, in full_dispatch_request 2018-04-18T13:33:38.831352063Z rv = self.handle_user_exception(e) 2018-04-18T13:33:38.831357531Z File "/usr/lib/python2.7/site-packages/flask/app.py", line 1517, in handle_user_exception 2018-04-18T13:33:38.83136302Z
reraise(exc_type, exc_value, tb) 2018-04-18T13:33:38.831367481Z File "/usr/lib/python2.7/site-packages/flask/app.py", line 1612, in full_dispatch_request 2018-04-18T13:33:38.831372038Z rv = self.dispatch_request() 2018-04-18T13:33:38.831377673Z File "/usr/lib/python2.7/site-packages/flask/app.py", line 1598, in dispatch_request 2018-04-18T13:33:38.831385927Z return self.view_functionsrule.endpoint 2018-04-18T13:33:38.831391913Z File "/src/simpleapp.py", line 14, in parse_xml 2018-04-18T13:33:38.831396571Z content_dict = xmltodict.parse(request.data) 2018-04-18T13:33:38.831401027Z File "/usr/lib/python2.7/site-packages/xmltodict.py", line 330, in parse 2018-04-18T13:33:38.831407166Z parser.Parse(xml_input, True) 2018-04-18T13:33:38.831412259Z ExpatError: no element found: line 1, column 0 2018-04-18T13:33:38.831705887Z 100.96.2.171 - - [18/Apr/2018 13:33:38] "POST / HTTP/1.1" 500 -

Any help would be appreciated, thank you.

2 Answers 2

4

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

Curl:

 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)

Response:

{
  "xml": {
      "Body": "Hello, it worked!",
      "From": "Jack"
  }
}
Sign up to request clarification or add additional context in comments.

Comments

2

You could also use curl to post XML file content:

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

data.xml content:

<xml>
 <From>Jack</From>
 <Body>Hello, it worked!</Body>
</xml>

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.