Issue
I have created an interceptor
@Injectable()
export class HttpsRequestInterceptor implements HttpInterceptor {
intercept(
req: HttpRequest<any>,
next: HttpHandler
): Observable<HttpEvent<any>> {
console.log(req);
if(req.url == 'https://abcd.azure.net/api/v1/getPendinList') {
// return Observable.empty();
console.log('hello')
}
const dupReq = req.clone({
headers: req.headers.set('Consumer-Secret', 'some sample key')
});
return next.handle(dupReq);
}
}
This is working fine I get console.log whenever I hithttps://abcd.azure.net/api/v1/getPendinList
. What I am trying to acheive is that if I hit this url, I want to change this url into something else ,e.g. abcd.api.com/search
. So that my url fetch data from new endpoint.
Is this possible and how.
Solution
Yes, you can override URL with new HTTPREQUEST
or cloning. but you can't directly assign new URL because it's a constant/read-only property.
// Added these lines
// const httpRequest = new HttpRequest(<any>req.method, 'abcd.api.com/search');
// req = Object.assign(req, httpRequest);
@Injectable()
export class HttpsRequestInterceptor implements HttpInterceptor {
intercept(req: HttpRequest<any>, next: HttpHandler): Observable<HttpEvent<any>> {
console.log(req);
const httpRequest = new HttpRequest(<any>req.method, 'abcd.api.com/search');
req = Object.assign(req, httpRequest);
if (req.url === 'https://abcd.azure.net/api/v1/getPendinList') {
// return Observable.empty();
console.log('hello');
}
const dupReq = req.clone({
headers: req.headers.set('Consumer-Secret', 'some sample key'),
});
return next.handle(dupReq);
}
}
Answered By - ngLover
0 comments:
Post a Comment
Note: Only a member of this blog may post a comment.