(3) HTTP
接口之(3) 自定义返回 HTTP 状态码
1 场景
封装后端 HTTP 服务(作为代理转发)
单点登录认证,需要返回特定 HTTP 状态码服务
2 例子
import os
from xlib.httpgateway import Request
from xlib import retstat
__info = "http_demo"
__version = "1.0.1"
def hello(req, str_info):
"""
带参数请求例子
Args:
req : Request
str_info: (str)
Returns:
httpstatus, Content, headers
"""
isinstance(req, Request)
return retstat.HTTP_OK, '{"test":"xxx"}', [(__info, __version)]
return 说明:
httpstatus: 状态码(200, 404, 302, 403 等) 是个数字
Content: 支持字节流或者字典
传字典时,会默认转为 json 并使用 utf-8 编码,("Content-Type", "application/json")
传字节流时,("Content-Type", "text/html")
headers: 响应头
2.2 retstat 常见返回码
# 重定向
HTTP_REDIRECT = 302
# 未认证
HTTP_UNAUTHORIZED = 401
# 未授权
HTTP_FORBIDDEN = 403
3 其他问题
(1) Content 传 json 时,将会被识别为 ("Content-Type", "text/html") ,前端服务是否会无法自动转为 js 对象?
Last updated