场景一
http://127.0.0.1:8088/api/getblogbyany/?category=Django&authorname=ArithmeticJia类似这样的请求格式
注释掉的方法也可以
# url:http://127.0.0.1:8088/api/getblogbyany/?category=Django&authorname=ArithmeticJia
class GetBlogByAny(RequestHandler):
def get(self):
# category = self.get_query_argument('category')
# authorname = self.get_query_argument('authorname')
category = self.get_argument('category')
authorname = self.get_argument('authorname')
print(category, authorname)
# 适合API请求
(r'/api/getblogbyany/', GetBlogByAny),
场景二
http://127.0.0.1:8088/api/getblogbycategory/Django类似这样的请求
@basic_auth(basic_auth_valid)
class GetBlogByCategory(tornado.web.RequestHandler):
def get(self, category):
print(category)
(r'/api/getblogbycategory/(?P<category>.+)', GetBlogByCategory),
这里用到了正则表达式
多个参数可以这样
class GetPython(RequestHandler):
def get(self, name, age):
self.write('name:%s age:%s' % (name, age))
(r'/api/python/(?P<name>.+)/(?P<age>[0-9]+)', GetPython),
这里url里的参数名一定要和get函数里面对应,顺序无所谓,但是名字一定要一样 。