kratos的ctx转http的request

1,252 阅读1分钟

kratos中的ctx是原生的context.Context, 比如想要获取http的header,就得转化成http的request

func GetRequestFromCtx(ctx context.Context) (request *http.Request, getReqSuccess bool) {
        if transPort, ok := transport.FromServerContext(ctx); ok {
                if transPort.Kind() == transport.KindHTTP {
                        if info, ok := transPort.(*trHttp.Transport); ok {
                                request = info.Request()
                                getReqSuccess = true
                        }
                }
        }
        return
}