小编今天带大家了解flask和curl的用法是什么,文中知识点介绍的非常详细。觉得有帮助的朋友可以跟着小编一起浏览文章的内容,希望能够帮助更多想解决这个问题的朋友找到问题的答案,下面跟着小编一起深入学习“flask和curl的用法是什么”的知识吧。
,基本熟悉flask和curl的用法,于是写个程序:
__author__ = 'hochikong'
from flask import Flask,request
from flask.ext.restful import Resource,Api,reqparse
app = 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]},201
class GetName(Resource):
def post(self):
args = parser.parse_args()
name = {}
name['ac'] = args['name']
return name
#args = parser.parse_args()
#name = args['name']
#return name
api.add_resource(TodoSimple,'/<string:todo_id>')
api.add_resource(GetName,'/getname')
if __name__ == '__main__':
app.run()
注意代码中注释的部分,在使用reqparse时,如果按照注释部分写,会显示如下问题:
hochikong@hochikong-P41T-D3:~$ 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 connection 0
虽然是200,但是没有任何数据返回
我想,args['name']输出的的确是一个字符串,但是flask-RESTful的响应应该是:
Content-Type: application/json
而没有把单个字符串弄成json格式的途径
但一旦放入python字典中,return的结果就是一个合法的json输出.
我猜的哇 :)
感谢大家的阅读,以上就是“flask和curl的用法是什么”的全部内容了,学会的朋友赶紧操作起来吧。相信天达云小编一定会给大家带来更优质的文章。谢谢大家对天达云网站的支持!