Skip to content

Instantly share code, notes, and snippets.

@soasme
Created August 20, 2013 01:58
Show Gist options
  • Save soasme/6276304 to your computer and use it in GitHub Desktop.
Save soasme/6276304 to your computer and use it in GitHub Desktop.
# -*- coding: utf-8 -*-
from flask import Flask, request
from flask.ext.restful import Resource, Api
from flask.ext.restful import reqparse
app = Flask(__name__)
api = Api(app)
class Progress(Resource):
def put(self, id):
parser = reqparse.RequestParser()
parser.add_argument('progress', type=float, required=True, location="json")
args = parser.parse_args()
return {'id':id, 'progress':args['progress']}
api.add_resource(Progress, '/progress/<string:id>')
if __name__ == '__main__':
app.run(debug=True)
'''
~ % curl http://127.0.0.1:5000/progress/1 -d '{"progress":1.0}' -X PUT -H"Content-Type: application/json"
{
"id": "1",
"progress": 1.0
}
~ % curl http://127.0.0.1:5000/progress/1 -d '{"progress":0.0@}' -X PUT -H"Content-Type: application/json"
{
"message": "Bad Request",
"status": 400
}
~ % curl http://127.0.0.1:5000/progress/1 -d '{}' -X PUT -H"Content-Type: application/json"
{
"message": "progress is required in json"
}
'''
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment