Angular 2 http inteceptor doesn't work when using forRoot











up vote
-1
down vote

favorite












I have an http interceptor in my shared module. I need to share a service defined in the shared module as a singleton between multiple module, to do this I need to use forRoot in the app.module. However, when I do this my http interceptor no longer works.



Shared Module:



@NgModule({
imports: [
FormsModule,
ReactiveFormsModule,
CommonModule,
HttpModule
],
declarations: [...fromComponents.components, ...fromPipes.pipes, ...fromDirectives.directives],
providers: [
{
provide: Http,
useFactory: fromFactories.httpFactory,
deps: [XHRBackend, RequestOptions, BaseDataService]
},
...fromServices.services,
...fromGuards.guards],
exports: [...fromComponents.components, ...fromPipes.pipes, ...fromDirectives.directives]
})


App.module:



imports: [
SharedCoreModule.forRoot()
]


httpFactory:



export function httpFactory(xhrBackend: XHRBackend, requestOptions: RequestOptions, baseDataService: BaseDataService): Http {


return new InterceptedHttp(xhrBackend, requestOptions, baseDataService);
}



HttpInterceptor:



export class InterceptedHttp extends Http {

constructor(backend: ConnectionBackend, defaultOptions: RequestOptions, public baseDataService: BaseDataService) {
super(backend, defaultOptions);
}

request(url: string | Request): Observable<Response> {
return super.request(url);
}

get(url: string, options): Observable<Response> {
return super.get(url, this.getRequestOptionArgs(options));
}

post(url: string, body: string, options): Observable<Response> {
return super.post(url, body, this.getRequestOptionArgs(options));
}

put(url: string, body: string, options?: RequestOptionsArgs): Observable<Response> {
return super.put(url, body, this.getRequestOptionArgs(options));
}

delete(url: string, options?: RequestOptionsArgs): Observable<Response> {
return super.delete(url, this.getRequestOptionArgs(options));
}

private getRequestOptionArgs(options?: RequestOptionsArgs): RequestOptionsArgs {
return this.baseDataService.getRequestOptionsNew(options);
}

}


Does anyone know why my interceptor no longer works?










share|improve this question
























  • Interceptors don't work with the deprecated HttpModule, you need to use HttpClientModule. Also you don't actually seem to be including the interceptor.
    – jonrsharpe
    Nov 21 at 9:48










  • @jonrsharpe are you referring to Angular 2? As built in interceptors come in from Angular 4+. The above works when I don't import SharedModule with forRoot. The factory returns the interceptor.
    – dotnethaggis
    Nov 21 at 10:43










  • Please give a Minimal, Complete, and Verifiable example, then. Also I'd recommend not still using 2.x if that's actually what you're on.
    – jonrsharpe
    Nov 21 at 10:45










  • @jonrsharpe I have no choice about Angular 2... it's a working solution in a live production. Can't just upgrade from 2 - 7 :)
    – dotnethaggis
    Nov 21 at 10:50






  • 1




    You do, and you can. It would have been easier if you gradually upgraded instead of continuing to use a version that hasn't been updated in 18 months, but you can stop making that bad choice. See e.g. angular.io/guide/releases#support-policy-and-schedule - even 4 is out of LTS by now.
    – jonrsharpe
    Nov 21 at 10:53

















up vote
-1
down vote

favorite












I have an http interceptor in my shared module. I need to share a service defined in the shared module as a singleton between multiple module, to do this I need to use forRoot in the app.module. However, when I do this my http interceptor no longer works.



Shared Module:



@NgModule({
imports: [
FormsModule,
ReactiveFormsModule,
CommonModule,
HttpModule
],
declarations: [...fromComponents.components, ...fromPipes.pipes, ...fromDirectives.directives],
providers: [
{
provide: Http,
useFactory: fromFactories.httpFactory,
deps: [XHRBackend, RequestOptions, BaseDataService]
},
...fromServices.services,
...fromGuards.guards],
exports: [...fromComponents.components, ...fromPipes.pipes, ...fromDirectives.directives]
})


App.module:



imports: [
SharedCoreModule.forRoot()
]


httpFactory:



export function httpFactory(xhrBackend: XHRBackend, requestOptions: RequestOptions, baseDataService: BaseDataService): Http {


return new InterceptedHttp(xhrBackend, requestOptions, baseDataService);
}



HttpInterceptor:



export class InterceptedHttp extends Http {

constructor(backend: ConnectionBackend, defaultOptions: RequestOptions, public baseDataService: BaseDataService) {
super(backend, defaultOptions);
}

request(url: string | Request): Observable<Response> {
return super.request(url);
}

get(url: string, options): Observable<Response> {
return super.get(url, this.getRequestOptionArgs(options));
}

post(url: string, body: string, options): Observable<Response> {
return super.post(url, body, this.getRequestOptionArgs(options));
}

put(url: string, body: string, options?: RequestOptionsArgs): Observable<Response> {
return super.put(url, body, this.getRequestOptionArgs(options));
}

delete(url: string, options?: RequestOptionsArgs): Observable<Response> {
return super.delete(url, this.getRequestOptionArgs(options));
}

private getRequestOptionArgs(options?: RequestOptionsArgs): RequestOptionsArgs {
return this.baseDataService.getRequestOptionsNew(options);
}

}


Does anyone know why my interceptor no longer works?










share|improve this question
























  • Interceptors don't work with the deprecated HttpModule, you need to use HttpClientModule. Also you don't actually seem to be including the interceptor.
    – jonrsharpe
    Nov 21 at 9:48










  • @jonrsharpe are you referring to Angular 2? As built in interceptors come in from Angular 4+. The above works when I don't import SharedModule with forRoot. The factory returns the interceptor.
    – dotnethaggis
    Nov 21 at 10:43










  • Please give a Minimal, Complete, and Verifiable example, then. Also I'd recommend not still using 2.x if that's actually what you're on.
    – jonrsharpe
    Nov 21 at 10:45










  • @jonrsharpe I have no choice about Angular 2... it's a working solution in a live production. Can't just upgrade from 2 - 7 :)
    – dotnethaggis
    Nov 21 at 10:50






  • 1




    You do, and you can. It would have been easier if you gradually upgraded instead of continuing to use a version that hasn't been updated in 18 months, but you can stop making that bad choice. See e.g. angular.io/guide/releases#support-policy-and-schedule - even 4 is out of LTS by now.
    – jonrsharpe
    Nov 21 at 10:53















up vote
-1
down vote

favorite









up vote
-1
down vote

favorite











I have an http interceptor in my shared module. I need to share a service defined in the shared module as a singleton between multiple module, to do this I need to use forRoot in the app.module. However, when I do this my http interceptor no longer works.



Shared Module:



@NgModule({
imports: [
FormsModule,
ReactiveFormsModule,
CommonModule,
HttpModule
],
declarations: [...fromComponents.components, ...fromPipes.pipes, ...fromDirectives.directives],
providers: [
{
provide: Http,
useFactory: fromFactories.httpFactory,
deps: [XHRBackend, RequestOptions, BaseDataService]
},
...fromServices.services,
...fromGuards.guards],
exports: [...fromComponents.components, ...fromPipes.pipes, ...fromDirectives.directives]
})


App.module:



imports: [
SharedCoreModule.forRoot()
]


httpFactory:



export function httpFactory(xhrBackend: XHRBackend, requestOptions: RequestOptions, baseDataService: BaseDataService): Http {


return new InterceptedHttp(xhrBackend, requestOptions, baseDataService);
}



HttpInterceptor:



export class InterceptedHttp extends Http {

constructor(backend: ConnectionBackend, defaultOptions: RequestOptions, public baseDataService: BaseDataService) {
super(backend, defaultOptions);
}

request(url: string | Request): Observable<Response> {
return super.request(url);
}

get(url: string, options): Observable<Response> {
return super.get(url, this.getRequestOptionArgs(options));
}

post(url: string, body: string, options): Observable<Response> {
return super.post(url, body, this.getRequestOptionArgs(options));
}

put(url: string, body: string, options?: RequestOptionsArgs): Observable<Response> {
return super.put(url, body, this.getRequestOptionArgs(options));
}

delete(url: string, options?: RequestOptionsArgs): Observable<Response> {
return super.delete(url, this.getRequestOptionArgs(options));
}

private getRequestOptionArgs(options?: RequestOptionsArgs): RequestOptionsArgs {
return this.baseDataService.getRequestOptionsNew(options);
}

}


Does anyone know why my interceptor no longer works?










share|improve this question















I have an http interceptor in my shared module. I need to share a service defined in the shared module as a singleton between multiple module, to do this I need to use forRoot in the app.module. However, when I do this my http interceptor no longer works.



Shared Module:



@NgModule({
imports: [
FormsModule,
ReactiveFormsModule,
CommonModule,
HttpModule
],
declarations: [...fromComponents.components, ...fromPipes.pipes, ...fromDirectives.directives],
providers: [
{
provide: Http,
useFactory: fromFactories.httpFactory,
deps: [XHRBackend, RequestOptions, BaseDataService]
},
...fromServices.services,
...fromGuards.guards],
exports: [...fromComponents.components, ...fromPipes.pipes, ...fromDirectives.directives]
})


App.module:



imports: [
SharedCoreModule.forRoot()
]


httpFactory:



export function httpFactory(xhrBackend: XHRBackend, requestOptions: RequestOptions, baseDataService: BaseDataService): Http {


return new InterceptedHttp(xhrBackend, requestOptions, baseDataService);
}



HttpInterceptor:



export class InterceptedHttp extends Http {

constructor(backend: ConnectionBackend, defaultOptions: RequestOptions, public baseDataService: BaseDataService) {
super(backend, defaultOptions);
}

request(url: string | Request): Observable<Response> {
return super.request(url);
}

get(url: string, options): Observable<Response> {
return super.get(url, this.getRequestOptionArgs(options));
}

post(url: string, body: string, options): Observable<Response> {
return super.post(url, body, this.getRequestOptionArgs(options));
}

put(url: string, body: string, options?: RequestOptionsArgs): Observable<Response> {
return super.put(url, body, this.getRequestOptionArgs(options));
}

delete(url: string, options?: RequestOptionsArgs): Observable<Response> {
return super.delete(url, this.getRequestOptionArgs(options));
}

private getRequestOptionArgs(options?: RequestOptionsArgs): RequestOptionsArgs {
return this.baseDataService.getRequestOptionsNew(options);
}

}


Does anyone know why my interceptor no longer works?







angular typescript






share|improve this question















share|improve this question













share|improve this question




share|improve this question








edited Nov 21 at 10:52

























asked Nov 21 at 9:46









dotnethaggis

796922




796922












  • Interceptors don't work with the deprecated HttpModule, you need to use HttpClientModule. Also you don't actually seem to be including the interceptor.
    – jonrsharpe
    Nov 21 at 9:48










  • @jonrsharpe are you referring to Angular 2? As built in interceptors come in from Angular 4+. The above works when I don't import SharedModule with forRoot. The factory returns the interceptor.
    – dotnethaggis
    Nov 21 at 10:43










  • Please give a Minimal, Complete, and Verifiable example, then. Also I'd recommend not still using 2.x if that's actually what you're on.
    – jonrsharpe
    Nov 21 at 10:45










  • @jonrsharpe I have no choice about Angular 2... it's a working solution in a live production. Can't just upgrade from 2 - 7 :)
    – dotnethaggis
    Nov 21 at 10:50






  • 1




    You do, and you can. It would have been easier if you gradually upgraded instead of continuing to use a version that hasn't been updated in 18 months, but you can stop making that bad choice. See e.g. angular.io/guide/releases#support-policy-and-schedule - even 4 is out of LTS by now.
    – jonrsharpe
    Nov 21 at 10:53




















  • Interceptors don't work with the deprecated HttpModule, you need to use HttpClientModule. Also you don't actually seem to be including the interceptor.
    – jonrsharpe
    Nov 21 at 9:48










  • @jonrsharpe are you referring to Angular 2? As built in interceptors come in from Angular 4+. The above works when I don't import SharedModule with forRoot. The factory returns the interceptor.
    – dotnethaggis
    Nov 21 at 10:43










  • Please give a Minimal, Complete, and Verifiable example, then. Also I'd recommend not still using 2.x if that's actually what you're on.
    – jonrsharpe
    Nov 21 at 10:45










  • @jonrsharpe I have no choice about Angular 2... it's a working solution in a live production. Can't just upgrade from 2 - 7 :)
    – dotnethaggis
    Nov 21 at 10:50






  • 1




    You do, and you can. It would have been easier if you gradually upgraded instead of continuing to use a version that hasn't been updated in 18 months, but you can stop making that bad choice. See e.g. angular.io/guide/releases#support-policy-and-schedule - even 4 is out of LTS by now.
    – jonrsharpe
    Nov 21 at 10:53


















Interceptors don't work with the deprecated HttpModule, you need to use HttpClientModule. Also you don't actually seem to be including the interceptor.
– jonrsharpe
Nov 21 at 9:48




Interceptors don't work with the deprecated HttpModule, you need to use HttpClientModule. Also you don't actually seem to be including the interceptor.
– jonrsharpe
Nov 21 at 9:48












@jonrsharpe are you referring to Angular 2? As built in interceptors come in from Angular 4+. The above works when I don't import SharedModule with forRoot. The factory returns the interceptor.
– dotnethaggis
Nov 21 at 10:43




@jonrsharpe are you referring to Angular 2? As built in interceptors come in from Angular 4+. The above works when I don't import SharedModule with forRoot. The factory returns the interceptor.
– dotnethaggis
Nov 21 at 10:43












Please give a Minimal, Complete, and Verifiable example, then. Also I'd recommend not still using 2.x if that's actually what you're on.
– jonrsharpe
Nov 21 at 10:45




Please give a Minimal, Complete, and Verifiable example, then. Also I'd recommend not still using 2.x if that's actually what you're on.
– jonrsharpe
Nov 21 at 10:45












@jonrsharpe I have no choice about Angular 2... it's a working solution in a live production. Can't just upgrade from 2 - 7 :)
– dotnethaggis
Nov 21 at 10:50




@jonrsharpe I have no choice about Angular 2... it's a working solution in a live production. Can't just upgrade from 2 - 7 :)
– dotnethaggis
Nov 21 at 10:50




1




1




You do, and you can. It would have been easier if you gradually upgraded instead of continuing to use a version that hasn't been updated in 18 months, but you can stop making that bad choice. See e.g. angular.io/guide/releases#support-policy-and-schedule - even 4 is out of LTS by now.
– jonrsharpe
Nov 21 at 10:53






You do, and you can. It would have been easier if you gradually upgraded instead of continuing to use a version that hasn't been updated in 18 months, but you can stop making that bad choice. See e.g. angular.io/guide/releases#support-policy-and-schedule - even 4 is out of LTS by now.
– jonrsharpe
Nov 21 at 10:53



















active

oldest

votes











Your Answer






StackExchange.ifUsing("editor", function () {
StackExchange.using("externalEditor", function () {
StackExchange.using("snippets", function () {
StackExchange.snippets.init();
});
});
}, "code-snippets");

StackExchange.ready(function() {
var channelOptions = {
tags: "".split(" "),
id: "1"
};
initTagRenderer("".split(" "), "".split(" "), channelOptions);

StackExchange.using("externalEditor", function() {
// Have to fire editor after snippets, if snippets enabled
if (StackExchange.settings.snippets.snippetsEnabled) {
StackExchange.using("snippets", function() {
createEditor();
});
}
else {
createEditor();
}
});

function createEditor() {
StackExchange.prepareEditor({
heartbeatType: 'answer',
convertImagesToLinks: true,
noModals: true,
showLowRepImageUploadWarning: true,
reputationToPostImages: 10,
bindNavPrevention: true,
postfix: "",
imageUploader: {
brandingHtml: "Powered by u003ca class="icon-imgur-white" href="https://imgur.com/"u003eu003c/au003e",
contentPolicyHtml: "User contributions licensed under u003ca href="https://creativecommons.org/licenses/by-sa/3.0/"u003ecc by-sa 3.0 with attribution requiredu003c/au003e u003ca href="https://stackoverflow.com/legal/content-policy"u003e(content policy)u003c/au003e",
allowUrls: true
},
onDemand: true,
discardSelector: ".discard-answer"
,immediatelyShowMarkdownHelp:true
});


}
});














draft saved

draft discarded


















StackExchange.ready(
function () {
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53409213%2fangular-2-http-inteceptor-doesnt-work-when-using-forroot%23new-answer', 'question_page');
}
);

Post as a guest















Required, but never shown






























active

oldest

votes













active

oldest

votes









active

oldest

votes






active

oldest

votes
















draft saved

draft discarded




















































Thanks for contributing an answer to Stack Overflow!


  • Please be sure to answer the question. Provide details and share your research!

But avoid



  • Asking for help, clarification, or responding to other answers.

  • Making statements based on opinion; back them up with references or personal experience.


To learn more, see our tips on writing great answers.





Some of your past answers have not been well-received, and you're in danger of being blocked from answering.


Please pay close attention to the following guidance:


  • Please be sure to answer the question. Provide details and share your research!

But avoid



  • Asking for help, clarification, or responding to other answers.

  • Making statements based on opinion; back them up with references or personal experience.


To learn more, see our tips on writing great answers.




draft saved


draft discarded














StackExchange.ready(
function () {
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53409213%2fangular-2-http-inteceptor-doesnt-work-when-using-forroot%23new-answer', 'question_page');
}
);

Post as a guest















Required, but never shown





















































Required, but never shown














Required, but never shown












Required, but never shown







Required, but never shown

































Required, but never shown














Required, but never shown












Required, but never shown







Required, but never shown







Popular posts from this blog

Berounka

Sphinx de Gizeh

Different font size/position of beamer's navigation symbols template's content depending on regular/plain...