Skip to content

Instantly share code, notes, and snippets.

@hanxue
Last active August 19, 2021 07:26
Show Gist options
  • Save hanxue/3666ccc8a7b69bb7ae75b44f30fc3230 to your computer and use it in GitHub Desktop.
Save hanxue/3666ccc8a7b69bb7ae75b44f30fc3230 to your computer and use it in GitHub Desktop.
Flask-restful with POST and PATCH methods
from flask import Flask
from flask_restful import Resource, Api, reqparse
app = Flask(__name__)
api = Api(app)
parser = reqparse.RequestParser()
class PostAndPatch(Resource):
def post(self):
parser.add_argument('shot_id', required=True, location='json', type=int,
help="Must have a shot id.")
parser.add_argument('shot_stage_id', required=True, location='json', type=int,
help="Must have the current stage id.")
parser.add_argument('shot_status_id', required=True, location='json', type=int,
help="Must have a status id.")
parser.add_argument('staff_id', required=True, location='json', type=int,
help="Must have a staff id.")
parser.add_argument('review_time', required=True, location='json',
help="Must have a review time, which should be right now.")
parser.add_argument('shot_version_name', required=True, location='json',
help="Must have a shot version name.")
parser.add_argument('shot_version_review_path', required=True, location='json',
help="Must have a shot version review path.")
args = parser.parse_args()
return {'method': 'This is a POST request'}
def patch(self):
parser.add_argument('shot_id', required=True, location='json', type=int,
help="Must have a shot id.")
parser.add_argument('shot_stage_id', required=True, location='json', type=int,
help="Must have the current stage id.")
parser.add_argument('shot_status_id', required=True, location='json', type=int,
help="Must have a status id.")
parser.add_argument('staff_id', required=True, location='json', type=int,
help="Must have a staff id.")
parser.add_argument('review_time', required=True, location='json',
help="Must have a review time, which should be right now.")
parser.add_argument('shot_version_name', required=True, location='json',
help="Must have a shot version name.")
parser.add_argument('shot_version_review_path', required=True, location='json',
help="Must have a shot version review path.")
parser.add_argument('shot_version_id', required=True, location='json',
help="Must have a shot version id.")
args = parser.parse_args()
return {'method': 'This is a PATCH request'}
api.add_resource(PostAndPatch, '/')
if __name__ == '__main__':
app.run(debug=True)
$ http PATCH localhost:5000
HTTP/1.0 200 OK
Content-Length: 44
Content-Type: application/json
Date: Fri, 27 Oct 2017 13:35:41 GMT
Server: Werkzeug/0.12.2 Python/2.7.13
{
"method": "This is a PATCH request"
}
$ http POST localhost:5000
HTTP/1.0 200 OK
Content-Length: 43
Content-Type: application/json
Date: Fri, 27 Oct 2017 13:35:47 GMT
Server: Werkzeug/0.12.2 Python/2.7.13
{
"method": "This is a POST request"
}
$ http POST localhost:5000 shot_id=53 shot_stage_id=3 shot_status_id=7 staff_id=17 review_time=20171022 shot_version_name=Shot_1202_02 shot_version_review_path=/mount/nas02/projects/xx2/reviews
HTTP/1.0 200 OK
Content-Length: 43
Content-Type: application/json
Date: Fri, 27 Oct 2017 13:56:51 GMT
Server: Werkzeug/0.12.2 Python/2.7.13
{
"method": "This is a POST request"
}
$ http PATCH localhost:5000 shot_id=53 shot_stage_id=3 shot_status_id=7 status_id=2 staff_id=17 review_time=20171022 shot_version_name=Shot_1202_02 shot_version_review_path=/mount/nas02/projects/xx2/reviews shot_version_id=113
HTTP/1.0 200 OK
Content-Length: 44
Content-Type: application/json
Date: Fri, 27 Oct 2017 13:58:36 GMT
Server: Werkzeug/0.12.2 Python/2.7.13
{
"method": "This is a PATCH request"
}
$ http PATCH localhost:5000 shot_id=53 shot_stage_id=3 shot_status_id=7 status_id=2 staff_id=17 review_time=20171022 shot_version_review_path=/mount/nas02/projects/xx2/reviews shot_version_id=113
HTTP/1.0 400 BAD REQUEST
Content-Length: 89
Content-Type: application/json
Date: Fri, 27 Oct 2017 13:58:49 GMT
Server: Werkzeug/0.12.2 Python/2.7.13
{
"message": {
"shot_version_name": "Must have a shot version name."
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment