Adding the ability to post JSON
This commit is contained in:
parent
76e7fa4894
commit
a3e4610ad9
2 changed files with 24 additions and 2 deletions
|
@ -5,6 +5,14 @@ This is a simple web app used for an university projects.
|
||||||
It should provide a simple score board display
|
It should provide a simple score board display
|
||||||
for some benchmarking data which is gathered from another application.
|
for some benchmarking data which is gathered from another application.
|
||||||
|
|
||||||
|
## Posting data
|
||||||
|
|
||||||
|
For now, you can post data using curl.
|
||||||
|
|
||||||
|
'''
|
||||||
|
curl -H "Content-Type: application/json" -X POST -d '{"text":"Hello Darkness","score": 5000}' http://localhost:5000/upload
|
||||||
|
'''
|
||||||
|
|
||||||
## Requires:
|
## Requires:
|
||||||
|
|
||||||
* nosetests
|
* nosetests
|
||||||
|
|
|
@ -18,6 +18,7 @@
|
||||||
from src.models import Result
|
from src.models import Result
|
||||||
from src.models import db
|
from src.models import db
|
||||||
import flask
|
import flask
|
||||||
|
import json
|
||||||
import os
|
import os
|
||||||
|
|
||||||
|
|
||||||
|
@ -43,9 +44,22 @@ def create_test_databases():
|
||||||
db.session.commit()
|
db.session.commit()
|
||||||
|
|
||||||
|
|
||||||
@app.route("/upload")
|
@app.route("/upload", methods=['POST'])
|
||||||
def upload():
|
def upload():
|
||||||
return "Upload"
|
content = flask.request.get_json()
|
||||||
|
try:
|
||||||
|
text = flask.escape(content['text'])
|
||||||
|
score = int(content['score'])
|
||||||
|
except KeyError: # Json doesn't contain the keys we need.
|
||||||
|
return json.dumps({'success': False}), 400, {'ContentType': 'application/json'}
|
||||||
|
except TypeError: # The types from the json object are not correct.
|
||||||
|
return json.dumps({'success': False}), 400, {'ContentType': 'application/json'}
|
||||||
|
|
||||||
|
entry = Result(text=text, score=score)
|
||||||
|
db.session.add(entry)
|
||||||
|
db.session.commit()
|
||||||
|
|
||||||
|
return json.dumps({'success': True}), 200, {'ContentType': 'application/json'}
|
||||||
|
|
||||||
|
|
||||||
@app.route("/")
|
@app.route("/")
|
||||||
|
|
Loading…
Reference in a new issue