今天聊的是HTTP Get 方法中是否可以通过body 传值的问题:
一个问题:
定义了一个 http get 方法,能否在 request body 中添加请求参数?
下面的测试会表明,是可以在 get 方法中添加请求参数的。
“遗憾”的是,一些“工具”会做一些约束,从而让你很难触及 http 的精髓和本质...
一个代码片段 Django
class Login(BaseView):
"""Login.
"""
def get(self, request, format=None):
try:
res = {}
code = ErrorCode.SUCCESS
# TODO: refactor loads later
receive = json.loads(request.body)
username = receive.get('username', '')
password = receive.get('password', '')
logging.info("[USER] Login, args: %s.", receive)
except Exception as e:
code = ErrorCode.ILLEGAL_DATA_FORMAT
logging.exception(
"[USER] Illegal data, Exception: %s", e.args)
return self.write_res(code)
...
$ curl -i -X GET -d '{"name":"jia", "start_time":11}' http://172.28.20.124:7777/cmdb/openapi/login/
HTTP/1.1 200 OK
Content-Length: 66
Vary: Accept, Cookie
Server: TornadoServer/4.5.1
Allow: GET, HEAD, OPTIONS
X-Frame-Options: SAMEORIGIN
Content-Type: application/json
{"msg": "\u7528\u6237\u4e0d\u5b58\u5728", "code": 206, "data": {}}
# NOTE: output
[I 171226 21:53:20 open_views:34] [USER] Login, args: {u'start_time': 11, u'name': u'jia'}.
2017-12-26 21:53:20 root open_views.py [INFO] [USER] Login, args: {u'start_time': 11, u'name': u'jia'}.
[I 171226 21:53:20 wsgi:355] 200 GET /cmdb/openapi/login/ (172.28.32.49) 3409.83ms