如何在NestJs中使用拦截器修改来自PUT的请求和响应
我正在使用NestJs。我在控制器中使用拦截器进行PUT请求 我想在PUT请求之前更改请求主体,并更改通过PUT请求返回的响应主体。如何做到这一点 投入使用如何在NestJs中使用拦截器修改来自PUT的请求和响应,nestjs,Nestjs,我正在使用NestJs。我在控制器中使用拦截器进行PUT请求 我想在PUT请求之前更改请求主体,并更改通过PUT请求返回的响应主体。如何做到这一点 投入使用 @UseInterceptors(UpdateFlowInterceptor) @Put('flows') public updateFlow(@Body() flow: Flow): Observable<Flow> { return this.apiFactory.getApiService().upda
@UseInterceptors(UpdateFlowInterceptor)
@Put('flows')
public updateFlow(@Body() flow: Flow): Observable<Flow> {
return this.apiFactory.getApiService().updateFlow(flow).pipe(catchError(error =>
of(new HttpException(error.message, 404))));
}
@UseInterceptors(UpdateFlowInterceptor)
@Put('流')
public updateFlow(@Body()flow:flow):可观察{
返回此.apifacture.getApiService().updateFlow(flow).pipe(catchError)(错误=>
of(新的HttpException(error.message,404));
}
拦截器
@Injectable()
export class UpdateFlowInterceptor implements NestInterceptor {
public intercept(_context: ExecutionContext, next: CallHandler): Observable<FlowUI> {
// how to change request also
return next.handle().pipe(
map(flow => {
flow.name = 'changeing response body';
return flow;
}),
);
}
}
@Injectable()
导出类UpdateFlowInterceptor实现NestInterceptor{
公共截获(_context:ExecutionContext,next:CallHandler):可观察{
//如何更改请求也
返回next.handle().pipe(
映射(流=>{
flow.name='changing response body';
回流;
}),
);
}
}
我可以通过从ExecutionContext
下面是代码
@Injectable()
export class UpdateFlowInterceptor implements NestInterceptor {
public intercept(_context: ExecutionContext, next: CallHandler): Observable<FlowUI> {
// changing request
let request = _context.switchToHttp().getRequest();
if (request.body.name) {
request.body.name = 'modify request';
}
return next.handle().pipe(
map(flow => {
flow.name = 'changeing response body';
return flow;
}),
);
}
}
@Injectable()
导出类UpdateFlowInterceptor实现NestInterceptor{
公共截获(_context:ExecutionContext,next:CallHandler):可观察{
//更改请求
let request=_context.switchToHttp().getRequest();
if(request.body.name){
request.body.name='修改请求';
}
返回next.handle().pipe(
映射(流=>{
flow.name='changing response body';
回流;
}),
);
}
}