if (req.method === 'GET' && req.params.has('app_id')) {
return this.myAppListService.myListObservable.switchMap(() => {
let appId = this.myAppListService.selectedAppId;
newParams = req.params.set('app_id', appId);
const comReq = req.clone({
params: newParams,
body: newBody
});
return next.handle(comReq).do((res) => {
this.handleResponse(res);
});
});
}
if (req.method === 'POST' && req.headers.get('content-type').indexOf('application/x-www-form-urlencoded;') > -1) {
if (req.body.indexOf('app_id=')) {
return this.myAppListService.myListObservable.switchMap(() => {
let appId = this.myAppListService.selectedAppId;
newBody = req.body.replace(req.body.match(/(^|&)app_id=([^&]*)(&|$)/)[0], '');
newBody = newBody + '&app_id=' + this.myAppListService.selectedAppId;
const comReq = req.clone({
params: newParams,
body: newBody
});
return next.handle(comReq).do((res) => {
this.handleResponse(res);
});
});
}
}
本文探讨了在HTTP请求处理中如何根据请求方法和内容类型动态修改请求参数,特别是如何注入或替换'app_id'参数,确保应用程序ID的正确传递。通过使用RxJS的switchMap操作符和自定义的请求克隆方法,实现对GET和POST请求的灵活控制。
293

被折叠的 条评论
为什么被折叠?



