In addition to Weibo, there is also WeChat
Please pay attention
WeChat public account
Shulou
2025-01-16 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Servers >
Share
Shulou(Shulou.com)05/31 Report--
Today, I will talk to you about the design details and implementation of restapi, which may not be well understood by many people. In order to make you understand better, the editor has summarized the following contents for you. I hope you can gain something according to this article.
In the design of RESTful API, I intend to customize a request header and put token in it to request services from other sora components.
So, change the previous code a little bit like this:
Parser.add_argument ('auth-token',type=str,help='put the token here',location='headers')
When referencing this value, the usage is as follows:
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): Todos [Todo _ id] = request.form ['data'] return {todo_id: Todo [Todo _ id]}
direct
Args = parser.parse_args ()
Then read the value in it.
In addition, in the previous test, I simply used-d to specify "name=hochikong" to manipulate the getname resource, and now change it slightly.
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}
But curl's request looks like this:
Curl-I-X POST-H 'Content-Type:application/json'-d' {"name": "hochikong"} 'http://localhost:5000/getname
Be careful! I am sending JSON data, so I want to change http head to application/json. In addition:
'{"name": "hochikong"}'
Strings in JSON data should be in double quotation marks, otherwise an error will be reported. Besides JSON data, you need to enclose a quotation mark.
My complete 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') # because the phrase "By default, the RequestParser tries to parse values from flask.Request.values, and flask.Request.json." # We don't need to add 'location=json'' after the parameter name But it wouldn't hurt to add 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: Todo _ id]} else: return {' error':'token error'} 401 def put (self Todo_id): todo_id [todo _ id] = request.form ['data'] return {todo_id: todo [Todo _ id]} 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 '/') api.add_resource (GetName,'/getname') if _ _ name__ = ='_ main__': app.run ()
Start:
Python flaskrr.py
Send a request to test the getname resource:
Hochikong@hochikong-P41T-D3:~/PycharmProjects/untitled/sora_test$ curl-I-X POST-H 'Content-Type:application/json'-d'{"name": "hochikong"} 'http://localhost:5000/getnameHTTP/1.0 200 OKContent-Type: application/jsonContent-Length: 24Server: Werkzeug/0.10.1 Python/2.7.6Date: Sat, 11 Apr 2015 14:07:03 GMT {"yourame": "hochikong"} hochikong@hochikong-P41T-D3:~/PycharmProjects/untitled/sora_test$
Send a request to test a custom head:
Hochikong@hochikong-P41T-D3:~/PycharmProjects/untitled/sora_test$ curl-X GET-H 'auth-token:thisismytoken' http://localhost:5000/task{"task": "get the list of docker"} hochikong@hochikong-P41T-D3:~/PycharmProjects/untitled/sora_test$
If the token is wrong:
Hochikong@hochikong-P41T-D3:~/PycharmProjects/untitled/sora_test$ curl-X GET-H 'auth-token:thisisyourtoken' http://localhost:5000/task{"error": "token error"} hochikong@hochikong-P41T-D3:~/PycharmProjects/untitled/sora_test$
The test was successful.
However, the hardest thing to design RESTful API is to design JSON request format. I am also drunk with all kinds of functions and formats.
Add:
Externally visible server.
After running the server, you will find that only your own computer can use the service, but not other computers in the network. This is the default setting, because users of the application can execute arbitrary Python code on your computer in debug mode.
If you turn off debugging or trust users on your network, you can make the server publicly accessible. Just change the call to the run () method like this:
App.run (host='0.0.0.0')
This code tells your operating system to listen on a public IP.
After reading the above, do you have any further understanding of the design details and implementation of restapi? If you want to know more knowledge or related content, please follow the industry information channel, thank you for your support.
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: 0
*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.