In addition to Weibo, there is also WeChat
Please pay attention
WeChat public account
Shulou
2025-04-01 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Servers >
Share
Shulou(Shulou.com)05/31 Report--
This article shows you how to use multiple http headers in flask applications and POST to submit data with the help of PUT. The content is concise and easy to understand, which will definitely brighten your eyes. I hope you can get something through the detailed introduction of this article.
Experimental purpose: to use multiple http headers in flask applications and submit data with PUT,POST
Source code:
_ _ author__ = 'hochikong'from flask import Flask,requestfrom flask.ext.restful import Resource,Api,reqparseapp = 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'} 401class 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/') api.add_resource (GetName) '/ getname') if _ _ name__ = =' _ main__': app.run (debug=True)
The core of the test is the TodoSimple class, and the test is the PUT method
The testing tool has been changed from simple curl to firefox REST client (in other words, I really don't have the patience to use curl directly). For more information on installing firefox extensions, please see here: http://www.blogjava.net/paulwong/archive/2014/04/19/412688.html.
Design requirements: to include content-type:application/json and custom auth-token headers in the HTTP request, and the request body is a json document, flask applications should know how to process the data in it.
Code analysis:
Class TodoSimple (Resource): def get (self,todo_id): args = parser.parse_args () if args ['auth-token'] = =' thisismytoken': return {todo_id: 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
From my previous blog, I know that you can parse json data with reqparse, and in the TodoSimple class, parsing auth-token headers is done using reqparse, but we need to define this parameter earlier:
Parser.add_argument ('auth-token',type=str,help='put the token here',location='headers')
If we don't use reqparse parsing, we can use the request module that comes with flask to parse:
Todo [Todo _ id] = request.json.get ('data')
And here's what I'm asking:
In this tool, Body no longer has to put quotation marks around curly braces as it does in curl, and it's much easier to define http headers.
With request.json.get (), flask applications can parse multiple http headers and json data.
The corresponding curl request is:
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/abcHTTP/1.0 201CREATEDContent-Type: application/jsonContent-Length: 68Server: Werkzeug/0.10.1 Python/2.7.6Date: Sat, 18 Apr 2015 15:10:46 GMT {"abc": "hello world" "task": "get the list of docker"}
OK, this experiment is basically successful. The next step is to distribute various resources in different python files and organize the placement of the code.
Pits encountered in the course of the study:
When debug=True is enabled, launch the flask application with an error:
ImportError: No module named _ winreg
Later, I revised six.py (http://www.cnblogs.com/lvzwq/p/4267850.html) with reference to this man's article.
Change it to this:
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
So there's no problem. This is a bug.
The above is how to use multiple http headers in flask applications and POST to submit data with the help of PUT. Have you learned any knowledge or skills? If you want to learn more skills or enrich your knowledge reserve, you are welcome to follow the industry information channel.
Welcome to subscribe "Shulou Technology Information " to get latest news, interesting things and hot topics in the IT industry, and controls the hottest and latest Internet news, technology news and IT industry trends.
Views: 254
*The comments in the above article only represent the author's personal views and do not represent the views and positions of this website. If you have more insights, please feel free to contribute and share.
Continue with the installation of the previous hadoop.First, install zookooper1. Decompress zookoope
"Every 5-10 years, there's a rare product, a really special, very unusual product that's the most un
© 2024 shulou.com SLNews company. All rights reserved.