温馨提示×

温馨提示×

您好,登录后才能下订单哦!

密码登录×
登录注册×
其他方式登录
点击 登录注册 即表示同意《亿速云用户服务条款》

如何在flask应用中使用多个http头并借助PUT实现POST提交数据

发布时间:2022-01-14 16:10:23 来源:亿速云 阅读:138 作者:柒染 栏目:云计算

本篇文章为大家展示了如何在flask应用中使用多个http头并借助PUT实现POST提交数据,内容简明扼要并且容易理解,绝对能使你眼前一亮,通过这篇文章的详细介绍希望你能有所收获。

实验目的:在flask应用中使用多个http头并借助PUT,POST提交数据

源代码:

__author__ = 'hochikong'
from flask import Flask,request
from flask.ext.restful import Resource,Api,reqparse


app = Flask(__name__)
api = Api(app)

todos = {'task':'get the list of docker'}

parser = reqparse.RequestParser()
parser.add_argument('name',type=str,help='get the name')
parser.add_argument('auth-token',type=str,help='put the token here',location='headers')


class TodoSimple(Resource):
    def get(self,todo_id):
        args = parser.parse_args()
        if args['auth-token'] == 'thisismytoken':
            return {todo_id:todos[todo_id]}
        else:
            return {'error':'token error'},401

    def put(self,todo_id):
        args = parser.parse_args()
        if args['auth-token'] == 'thisismytoken':
            todos[todo_id] = request.json.get('data')
            return todos,201
        else:
            return {'status':'error'},401



class GetName(Resource):
    def post(self):
        args = parser.parse_args()
        name = args['name']
        #name = {}
        #name['ac'] = args['name']
        #name = request.json.get('name')
        return {'yourame':name}



api.add_resource(TodoSimple,'/todo/<string:todo_id>')
api.add_resource(GetName,'/getname')

if __name__ == '__main__':
    app.run(debug=True)

测试的核心是TodoSimple类,测试的是PUT方法

测试工具由单纯的curl换成了firefox的REST client(话说,直接用curl我也真没什么耐心去用),安装firefox扩展参考此:http://www.blogjava.net/paulwong/archive/2014/04/19/412688.html

设计要求:要在HTTP请求中,包含content-type:application/json和自定义的auth-token头,另外请求体是一个json文档,flask应用应该懂得处理其中的数据

代码分析:

class TodoSimple(Resource):
    def get(self,todo_id):
        args = parser.parse_args()
        if args['auth-token'] == 'thisismytoken':
            return {todo_id:todos[todo_id]}
        else:
            return {'error':'token error'},401
            
    def put(self,todo_id):
        args = parser.parse_args()
        if args['auth-token'] == 'thisismytoken':
            todos[todo_id] = request.json.get('data')
            return todos,201
        else:
            return {'status':'error'},401

从我前一篇blog中,我知道可以用reqparse解析json数据,在TodoSimple类中,解析auth-token头是使用reqparse进行的,但我们要在前面定义该参数:

parser.add_argument('auth-token',type=str,help='put the token here',location='headers')

如果我们不使用reqparse解析,我们可以用flask自带的request模块解析:

todos[todo_id] = request.json.get('data')

而我的请求是这样的:

如何在flask应用中使用多个http头并借助PUT实现POST提交数据

在这个工具中,Body不用再像在curl中那样在花括号外还要加引号,定义http头也方便多了。

通过request.json.get(),flask应用就可以解析多个http头和json数据了。

对应的curl请求为:

hochikong@hochikong-P41T-D3:~$ curl -i -X PUT -H 'Content-Type:application/json' -H 'auth-token:thisismytoken' -d '{"data":"hello world"}' http://localhost:5000/todo/abc
HTTP/1.0 201 CREATED
Content-Type: application/json
Content-Length: 68
Server: Werkzeug/0.10.1 Python/2.7.6
Date: Sat, 18 Apr 2015 15:10:46 GMT
{
    "abc": "hello world", 
    "task": "get the list of docker"
}

好,这次实验基本成功,下一步就是把各种资源分布在不同的python文件中,组织好代码的放置

研究过程中遇到的坑:

启用debug=True后,启动flask应用,提示错误:

ImportError: No module named _winreg

后来参考这位仁兄的文章修改了six.py(http://www.cnblogs.com/lvzwq/p/4267850.html),

改成这样:

        if attr in ("__file__", "__name__") and self.mod not in sys.modules:
 #           raise AttributeError
             raise AttributeError(attr)
        try:
             _module = self._resolve()
        except ImportError:
             raise AttributeError(attr)
 #       _module = self._resolve()
        value = getattr(_module, attr)
        setattr(self, attr, value)
        return value

于是就没问题了,这是一个bug

上述内容就是如何在flask应用中使用多个http头并借助PUT实现POST提交数据,你们学到知识或技能了吗?如果还想学到更多技能或者丰富自己的知识储备,欢迎关注亿速云行业资讯频道。

向AI问一下细节

免责声明:本站发布的内容(图片、视频和文字)以原创、转载和分享为主,文章观点不代表本网站立场,如果涉及侵权请联系站长邮箱:is@yisu.com进行举报,并提供相关证据,一经查实,将立刻删除涉嫌侵权内容。

AI