Last week to read the document, basically familiar with the use of flask and curl, so write a program:
__author__ = ' Hochikong ' from flask import flask,requestfrom flask.ext.restful import resource,api,reqparseapp = flask (__name__) Api = api (APP) todos = {}parser = reqparse. Requestparser () parser.add_argument (' name ', type=str,help= ' Get the name ') class todosimple ( Resource): def get (self,todo_id): Return {todo_id:todos[todo_id]} def put (self,todo_id): todos[todo_id] = request.form[' Data '] return {todo_id:todos[todo_id]},201class getname (Resource): def post (self): args = parser.parse_args () name = {} & nbsp;name[' ac '] = args[' name '] return name #args = Parser.parse_args () #name = args[' name '] #return nameapi.add_resource (todosimple, '/<string:todo_id> ') Api.add_resource (GetName, '/getname ') if __name__ == ' __main__ ': app.run ()
Note the part of the comment in the code that, when used with Reqparse, displays the following question if it is written in the comments section:
[email protected]:~$ curl http://localhost:5000/getname -d "Name=hochikong" -X POST -v* Hostname was NOT found in DNS cache* trying 127.0.0.1...* connected to localhost (127.0.0.1) port 5000 (#0) > post /getname http/1.1> user-agent: curl/7.35.0> host: localhost : 5000> accept: */*> content-length: 14> content-type: application/ X-www-form-urlencoded> * upload completely sent off: 14 out of 14 bytes* HTTP 1.0, assume close after body< HTTP/1.0 200 ok< content-type: application/json< content-length: 11< server: werkzeug/0.10.1 python/2.7.6< date: sat, 21 mar 2015 15:00:18 gmt< * closing&Nbsp;connection 0
Although it is 200, no data is returned
I think the output of args[' name ' is indeed a string, but the flask-restful response should be:
Content-type:application/json
Instead of making a single string into a JSON format
But once put into the Python dictionary, the result of return is a valid JSON output.
I guess, wow:)
#SORA some pits in #flask-restful