在 httpInterceptor 中设置参数覆盖了我想附加的额外参数
Set params in httpInterceptor is overriding extra params I want to append
当前行为:
我创建了一个 HttpInterceptor,如果可以获取用户凭据,它会为所有请求设置一个 id 和令牌。当我尝试在请求中设置额外的 HttpParam 时,它会被拦截器覆盖。
预期行为:
如果需要,希望能够设置额外的参数并让拦截器附加默认 ID 和令牌。
我明白为什么会这样,但是是否有解决方法让拦截器设置 http 参数并附加任何额外的请求参数?
import {
HttpEvent,
HttpInterceptor,
HttpHandler,
HttpRequest,
HttpParams,
} from '@angular/common/http';
import { AuthService } from './auth.service';
import { exhaustMap, take } from 'rxjs/operators';
@Injectable()
export class AuthInterceptorService implements HttpInterceptor {
constructor(private authService: AuthService) {}
intercept(req: HttpRequest<any>, next: HttpHandler) {
return this.authService.user.pipe(
take(1),
exhaustMap((user) => {
// For requests that do not need authentication, it will proceed without attaching params. E.g Login.
if (!user) {
return next.handle(req);
}
const modifiedReq = req.clone({
params: new HttpParams()
.set('id', user.id)
.set('token', user.token),
});
return next.handle(modifiedReq);
})
);
}
}
fetchCompaniesByUserId() {
return this.http
.get<any>(
`${environment.apiUrl}/GetCompaniesByUserId`,
{
params: new HttpParams().set('timestamp', Date.now().toString()),
}
).subscribe()
}
我自己没有尝试过,但您可以尝试附加到 req
中的现有参数。
export class AuthInterceptorService implements HttpInterceptor {
constructor(private authService: AuthService) {}
intercept(req: HttpRequest<any>, next: HttpHandler) {
return this.authService.user.pipe(
take(1),
exhaustMap((user) => {
// For requests that do not need authentication, it will proceed without attaching params. E.g Login.
if (!user) {
return next.handle(req);
}
const modifiedReq = req.clone({
params: req.params // <-- append to existing `params` in `req`
.set('id', user.id)
.set('token', user.token),
});
return next.handle(modifiedReq);
})
);
}
}
当前行为: 我创建了一个 HttpInterceptor,如果可以获取用户凭据,它会为所有请求设置一个 id 和令牌。当我尝试在请求中设置额外的 HttpParam 时,它会被拦截器覆盖。
预期行为: 如果需要,希望能够设置额外的参数并让拦截器附加默认 ID 和令牌。
我明白为什么会这样,但是是否有解决方法让拦截器设置 http 参数并附加任何额外的请求参数?
import {
HttpEvent,
HttpInterceptor,
HttpHandler,
HttpRequest,
HttpParams,
} from '@angular/common/http';
import { AuthService } from './auth.service';
import { exhaustMap, take } from 'rxjs/operators';
@Injectable()
export class AuthInterceptorService implements HttpInterceptor {
constructor(private authService: AuthService) {}
intercept(req: HttpRequest<any>, next: HttpHandler) {
return this.authService.user.pipe(
take(1),
exhaustMap((user) => {
// For requests that do not need authentication, it will proceed without attaching params. E.g Login.
if (!user) {
return next.handle(req);
}
const modifiedReq = req.clone({
params: new HttpParams()
.set('id', user.id)
.set('token', user.token),
});
return next.handle(modifiedReq);
})
);
}
}
fetchCompaniesByUserId() {
return this.http
.get<any>(
`${environment.apiUrl}/GetCompaniesByUserId`,
{
params: new HttpParams().set('timestamp', Date.now().toString()),
}
).subscribe()
}
我自己没有尝试过,但您可以尝试附加到 req
中的现有参数。
export class AuthInterceptorService implements HttpInterceptor {
constructor(private authService: AuthService) {}
intercept(req: HttpRequest<any>, next: HttpHandler) {
return this.authService.user.pipe(
take(1),
exhaustMap((user) => {
// For requests that do not need authentication, it will proceed without attaching params. E.g Login.
if (!user) {
return next.handle(req);
}
const modifiedReq = req.clone({
params: req.params // <-- append to existing `params` in `req`
.set('id', user.id)
.set('token', user.token),
});
return next.handle(modifiedReq);
})
);
}
}