#SORA some pits in #flask-restful

Source: Internet
Author: User

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

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.