最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

javascript - how to change HTTP Request URL using angular 6 interceptor - Stack Overflow

programmeradmin10浏览0评论

I am working on a angular app & fetching data from back end API endpoint with the get / post method of HTTP module. Recently I have checked a angular app where they are hiding API real endpoint & replacing with some other end point.

for e.g

Real Endpoint:- 'http://192.168.1.192:8080/restaurant/getUserData/'

Replaced Endpoint:- 'http://192.168.1.192:8080/restaurant/getServerData'

also I can see the replaced endpoint in the 'network' tab of 'Developer Tool'.

I know that this can be achieved by using interceptors concept. but I never worked with HTTP interceptor. if any one know how to achieve the same please suggest.

I am working on a angular app & fetching data from back end API endpoint with the get / post method of HTTP module. Recently I have checked a angular app where they are hiding API real endpoint & replacing with some other end point.

for e.g

Real Endpoint:- 'http://192.168.1.192:8080/restaurant/getUserData/'

Replaced Endpoint:- 'http://192.168.1.192:8080/restaurant/getServerData'

also I can see the replaced endpoint in the 'network' tab of 'Developer Tool'.

I know that this can be achieved by using interceptors concept. but I never worked with HTTP interceptor. if any one know how to achieve the same please suggest.

Share Improve this question edited Dec 18, 2019 at 10:43 Lakhan Khandelwal asked Dec 18, 2019 at 10:40 Lakhan KhandelwalLakhan Khandelwal 632 silver badges17 bronze badges 3
  • Please refer to angular.io/guide/http#http-interceptors – uajov6 Commented Dec 18, 2019 at 10:42
  • http interceptors are not used for this purpose generally. – Mridul Commented Dec 18, 2019 at 10:45
  • I would suggest you look at proxy angular.io/guide/build#proxying-to-a-backend-server – Bojan Kogoj Commented Dec 18, 2019 at 10:49
Add a ment  | 

2 Answers 2

Reset to default 4
@Injectable()
export class Interceptor implements HttpInterceptor {
 constructor() { }
 const redirectRequest = request.clone({ url: 'http://192.168.1.192:8080/restaurant/getServerData', method: "get" });
      return next.handle(redirectRequest);
}
@Injectable()
export class Interceptor implements HttpInterceptor {
  constructor() { }

  intercept(req: HttpRequest<any>, next: HttpHandler): Observable<HttpEvent<any>> {
    const dupReq = req.clone({ url: 'mynewurl.' });
    return next.handle(dupReq);
  }
}
发布评论

评论列表(0)

  1. 暂无评论