How submit Angular form with Http Interceptor










0















Do you know how can I submit my form using HttpInterceptor? My GET method it is ok using interceptor getting token and bringing the result etc... but when I try to submit my form nothing happen, the back-end is not being called.



TokenInterceptor.ts



@Injectable()
export class TokenInterceptor implements HttpInterceptor

constructor(private kcService: KeycloakService)

intercept(request: HttpRequest<any>, next: HttpHandler) : Observable<HttpEvent<any>>
return from(this.kcService.init())
.pipe(switchMap(authToken =>
debugger;
if(authToken)
const headers = request.headers
.set('Authorization', 'Bearer ' + authToken)
.append('Content-Type', 'application/json');
console.log(authToken);
const reqClone = request.clone(
headers
);
return next.handle(reqClone);

));





ItemService.ts



@Injectable()
export class ItemService

itensUrl = 'http://localhost:8080/itens'

constructor(private http: HttpClient, private kcService: KeycloakService)

list()
return this.http.get<any>(this.itensUrl);


addiction(item: any)
return this.http.post(this.itensUrl, item);




app.modules.ts:



 providers: [
ItemService,
KeycloakService,

provide: HTTP_INTERCEPTORS,
useClass: TokenInterceptor,
multi: true



ItemPatrimonyComponent.ts



export class ItemPatrimonyComponent implements OnInit {

itens = ;

constructor(private itemService: ItemService)

ngOnInit()
this.listAll();


listAll()
this.itemService.list().subscribe(data => this.itens = data)
console.log(this.itens);


add(frm:FormControl)
this.itemService.addiction(frm.value)
.subscribe(() =>
frm.reset();
this.listAll();
);










share|improve this question






















  • Sure, you have a if condition but no else. try providing one or providing a token.

    – trichetriche
    Nov 13 '18 at 15:02











  • thanks man but the problem is: looks my interceptor is intercepting GET but not POST, when I try to submit is not calling the back-end. I removed the if condition.

    – Neto
    Nov 13 '18 at 15:07











  • The interceptor intercepts every request. If your requests go into your interceptor, they will enter the process, don't satisfy the condition, and break the stream flow. you have to return next.handle for every request, and you don't, that's why you don't see your requests being made.

    – trichetriche
    Nov 13 '18 at 15:10












  • thanks again pal. Sorry but you mean: I need to change my intercept method ? I'm already returning "return next.handle(reqClone);"

    – Neto
    Nov 13 '18 at 15:15











  • You're only returning it in the if statement. What do you return when you don't have an authToken ? Where is the return statement ? I don't tell you to change your interceptor, I'm telling you to at least return something in all cases.

    – trichetriche
    Nov 13 '18 at 15:16















0















Do you know how can I submit my form using HttpInterceptor? My GET method it is ok using interceptor getting token and bringing the result etc... but when I try to submit my form nothing happen, the back-end is not being called.



TokenInterceptor.ts



@Injectable()
export class TokenInterceptor implements HttpInterceptor

constructor(private kcService: KeycloakService)

intercept(request: HttpRequest<any>, next: HttpHandler) : Observable<HttpEvent<any>>
return from(this.kcService.init())
.pipe(switchMap(authToken =>
debugger;
if(authToken)
const headers = request.headers
.set('Authorization', 'Bearer ' + authToken)
.append('Content-Type', 'application/json');
console.log(authToken);
const reqClone = request.clone(
headers
);
return next.handle(reqClone);

));





ItemService.ts



@Injectable()
export class ItemService

itensUrl = 'http://localhost:8080/itens'

constructor(private http: HttpClient, private kcService: KeycloakService)

list()
return this.http.get<any>(this.itensUrl);


addiction(item: any)
return this.http.post(this.itensUrl, item);




app.modules.ts:



 providers: [
ItemService,
KeycloakService,

provide: HTTP_INTERCEPTORS,
useClass: TokenInterceptor,
multi: true



ItemPatrimonyComponent.ts



export class ItemPatrimonyComponent implements OnInit {

itens = ;

constructor(private itemService: ItemService)

ngOnInit()
this.listAll();


listAll()
this.itemService.list().subscribe(data => this.itens = data)
console.log(this.itens);


add(frm:FormControl)
this.itemService.addiction(frm.value)
.subscribe(() =>
frm.reset();
this.listAll();
);










share|improve this question






















  • Sure, you have a if condition but no else. try providing one or providing a token.

    – trichetriche
    Nov 13 '18 at 15:02











  • thanks man but the problem is: looks my interceptor is intercepting GET but not POST, when I try to submit is not calling the back-end. I removed the if condition.

    – Neto
    Nov 13 '18 at 15:07











  • The interceptor intercepts every request. If your requests go into your interceptor, they will enter the process, don't satisfy the condition, and break the stream flow. you have to return next.handle for every request, and you don't, that's why you don't see your requests being made.

    – trichetriche
    Nov 13 '18 at 15:10












  • thanks again pal. Sorry but you mean: I need to change my intercept method ? I'm already returning "return next.handle(reqClone);"

    – Neto
    Nov 13 '18 at 15:15











  • You're only returning it in the if statement. What do you return when you don't have an authToken ? Where is the return statement ? I don't tell you to change your interceptor, I'm telling you to at least return something in all cases.

    – trichetriche
    Nov 13 '18 at 15:16













0












0








0








Do you know how can I submit my form using HttpInterceptor? My GET method it is ok using interceptor getting token and bringing the result etc... but when I try to submit my form nothing happen, the back-end is not being called.



TokenInterceptor.ts



@Injectable()
export class TokenInterceptor implements HttpInterceptor

constructor(private kcService: KeycloakService)

intercept(request: HttpRequest<any>, next: HttpHandler) : Observable<HttpEvent<any>>
return from(this.kcService.init())
.pipe(switchMap(authToken =>
debugger;
if(authToken)
const headers = request.headers
.set('Authorization', 'Bearer ' + authToken)
.append('Content-Type', 'application/json');
console.log(authToken);
const reqClone = request.clone(
headers
);
return next.handle(reqClone);

));





ItemService.ts



@Injectable()
export class ItemService

itensUrl = 'http://localhost:8080/itens'

constructor(private http: HttpClient, private kcService: KeycloakService)

list()
return this.http.get<any>(this.itensUrl);


addiction(item: any)
return this.http.post(this.itensUrl, item);




app.modules.ts:



 providers: [
ItemService,
KeycloakService,

provide: HTTP_INTERCEPTORS,
useClass: TokenInterceptor,
multi: true



ItemPatrimonyComponent.ts



export class ItemPatrimonyComponent implements OnInit {

itens = ;

constructor(private itemService: ItemService)

ngOnInit()
this.listAll();


listAll()
this.itemService.list().subscribe(data => this.itens = data)
console.log(this.itens);


add(frm:FormControl)
this.itemService.addiction(frm.value)
.subscribe(() =>
frm.reset();
this.listAll();
);










share|improve this question














Do you know how can I submit my form using HttpInterceptor? My GET method it is ok using interceptor getting token and bringing the result etc... but when I try to submit my form nothing happen, the back-end is not being called.



TokenInterceptor.ts



@Injectable()
export class TokenInterceptor implements HttpInterceptor

constructor(private kcService: KeycloakService)

intercept(request: HttpRequest<any>, next: HttpHandler) : Observable<HttpEvent<any>>
return from(this.kcService.init())
.pipe(switchMap(authToken =>
debugger;
if(authToken)
const headers = request.headers
.set('Authorization', 'Bearer ' + authToken)
.append('Content-Type', 'application/json');
console.log(authToken);
const reqClone = request.clone(
headers
);
return next.handle(reqClone);

));





ItemService.ts



@Injectable()
export class ItemService

itensUrl = 'http://localhost:8080/itens'

constructor(private http: HttpClient, private kcService: KeycloakService)

list()
return this.http.get<any>(this.itensUrl);


addiction(item: any)
return this.http.post(this.itensUrl, item);




app.modules.ts:



 providers: [
ItemService,
KeycloakService,

provide: HTTP_INTERCEPTORS,
useClass: TokenInterceptor,
multi: true



ItemPatrimonyComponent.ts



export class ItemPatrimonyComponent implements OnInit {

itens = ;

constructor(private itemService: ItemService)

ngOnInit()
this.listAll();


listAll()
this.itemService.list().subscribe(data => this.itens = data)
console.log(this.itens);


add(frm:FormControl)
this.itemService.addiction(frm.value)
.subscribe(() =>
frm.reset();
this.listAll();
);







angular interceptor angular-http-interceptors






share|improve this question













share|improve this question











share|improve this question




share|improve this question










asked Nov 13 '18 at 14:59









NetoNeto

376




376












  • Sure, you have a if condition but no else. try providing one or providing a token.

    – trichetriche
    Nov 13 '18 at 15:02











  • thanks man but the problem is: looks my interceptor is intercepting GET but not POST, when I try to submit is not calling the back-end. I removed the if condition.

    – Neto
    Nov 13 '18 at 15:07











  • The interceptor intercepts every request. If your requests go into your interceptor, they will enter the process, don't satisfy the condition, and break the stream flow. you have to return next.handle for every request, and you don't, that's why you don't see your requests being made.

    – trichetriche
    Nov 13 '18 at 15:10












  • thanks again pal. Sorry but you mean: I need to change my intercept method ? I'm already returning "return next.handle(reqClone);"

    – Neto
    Nov 13 '18 at 15:15











  • You're only returning it in the if statement. What do you return when you don't have an authToken ? Where is the return statement ? I don't tell you to change your interceptor, I'm telling you to at least return something in all cases.

    – trichetriche
    Nov 13 '18 at 15:16

















  • Sure, you have a if condition but no else. try providing one or providing a token.

    – trichetriche
    Nov 13 '18 at 15:02











  • thanks man but the problem is: looks my interceptor is intercepting GET but not POST, when I try to submit is not calling the back-end. I removed the if condition.

    – Neto
    Nov 13 '18 at 15:07











  • The interceptor intercepts every request. If your requests go into your interceptor, they will enter the process, don't satisfy the condition, and break the stream flow. you have to return next.handle for every request, and you don't, that's why you don't see your requests being made.

    – trichetriche
    Nov 13 '18 at 15:10












  • thanks again pal. Sorry but you mean: I need to change my intercept method ? I'm already returning "return next.handle(reqClone);"

    – Neto
    Nov 13 '18 at 15:15











  • You're only returning it in the if statement. What do you return when you don't have an authToken ? Where is the return statement ? I don't tell you to change your interceptor, I'm telling you to at least return something in all cases.

    – trichetriche
    Nov 13 '18 at 15:16
















Sure, you have a if condition but no else. try providing one or providing a token.

– trichetriche
Nov 13 '18 at 15:02





Sure, you have a if condition but no else. try providing one or providing a token.

– trichetriche
Nov 13 '18 at 15:02













thanks man but the problem is: looks my interceptor is intercepting GET but not POST, when I try to submit is not calling the back-end. I removed the if condition.

– Neto
Nov 13 '18 at 15:07





thanks man but the problem is: looks my interceptor is intercepting GET but not POST, when I try to submit is not calling the back-end. I removed the if condition.

– Neto
Nov 13 '18 at 15:07













The interceptor intercepts every request. If your requests go into your interceptor, they will enter the process, don't satisfy the condition, and break the stream flow. you have to return next.handle for every request, and you don't, that's why you don't see your requests being made.

– trichetriche
Nov 13 '18 at 15:10






The interceptor intercepts every request. If your requests go into your interceptor, they will enter the process, don't satisfy the condition, and break the stream flow. you have to return next.handle for every request, and you don't, that's why you don't see your requests being made.

– trichetriche
Nov 13 '18 at 15:10














thanks again pal. Sorry but you mean: I need to change my intercept method ? I'm already returning "return next.handle(reqClone);"

– Neto
Nov 13 '18 at 15:15





thanks again pal. Sorry but you mean: I need to change my intercept method ? I'm already returning "return next.handle(reqClone);"

– Neto
Nov 13 '18 at 15:15













You're only returning it in the if statement. What do you return when you don't have an authToken ? Where is the return statement ? I don't tell you to change your interceptor, I'm telling you to at least return something in all cases.

– trichetriche
Nov 13 '18 at 15:16





You're only returning it in the if statement. What do you return when you don't have an authToken ? Where is the return statement ? I don't tell you to change your interceptor, I'm telling you to at least return something in all cases.

– trichetriche
Nov 13 '18 at 15:16












1 Answer
1






active

oldest

votes


















0














The problem is on Keycloak init function, and you could not debug your problem because on your interceptor you are adding the debugger inside the callback of init instead of outside it.
If init is called and refresh the page (what keycloak does) you will never see the callback debugger working, what you need to do is on your keycloak service instead of call directly init verify if the token is already defined if it returns the token instead of call keycloak.init on each request.
Something like:



if (this.keycloak.token) 
return Promise.resolve(this.keycloak.token);

return this.keycloak.init().then(()=> this.keycloak.token)





share|improve this answer























  • Thanks Levy. Now it is working fine!!!

    – Neto
    Nov 13 '18 at 22:23










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',
autoActivateHeartbeat: false,
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%2f53283804%2fhow-submit-angular-form-with-http-interceptor%23new-answer', 'question_page');

);

Post as a guest















Required, but never shown

























1 Answer
1






active

oldest

votes








1 Answer
1






active

oldest

votes









active

oldest

votes






active

oldest

votes









0














The problem is on Keycloak init function, and you could not debug your problem because on your interceptor you are adding the debugger inside the callback of init instead of outside it.
If init is called and refresh the page (what keycloak does) you will never see the callback debugger working, what you need to do is on your keycloak service instead of call directly init verify if the token is already defined if it returns the token instead of call keycloak.init on each request.
Something like:



if (this.keycloak.token) 
return Promise.resolve(this.keycloak.token);

return this.keycloak.init().then(()=> this.keycloak.token)





share|improve this answer























  • Thanks Levy. Now it is working fine!!!

    – Neto
    Nov 13 '18 at 22:23















0














The problem is on Keycloak init function, and you could not debug your problem because on your interceptor you are adding the debugger inside the callback of init instead of outside it.
If init is called and refresh the page (what keycloak does) you will never see the callback debugger working, what you need to do is on your keycloak service instead of call directly init verify if the token is already defined if it returns the token instead of call keycloak.init on each request.
Something like:



if (this.keycloak.token) 
return Promise.resolve(this.keycloak.token);

return this.keycloak.init().then(()=> this.keycloak.token)





share|improve this answer























  • Thanks Levy. Now it is working fine!!!

    – Neto
    Nov 13 '18 at 22:23













0












0








0







The problem is on Keycloak init function, and you could not debug your problem because on your interceptor you are adding the debugger inside the callback of init instead of outside it.
If init is called and refresh the page (what keycloak does) you will never see the callback debugger working, what you need to do is on your keycloak service instead of call directly init verify if the token is already defined if it returns the token instead of call keycloak.init on each request.
Something like:



if (this.keycloak.token) 
return Promise.resolve(this.keycloak.token);

return this.keycloak.init().then(()=> this.keycloak.token)





share|improve this answer













The problem is on Keycloak init function, and you could not debug your problem because on your interceptor you are adding the debugger inside the callback of init instead of outside it.
If init is called and refresh the page (what keycloak does) you will never see the callback debugger working, what you need to do is on your keycloak service instead of call directly init verify if the token is already defined if it returns the token instead of call keycloak.init on each request.
Something like:



if (this.keycloak.token) 
return Promise.resolve(this.keycloak.token);

return this.keycloak.init().then(()=> this.keycloak.token)






share|improve this answer












share|improve this answer



share|improve this answer










answered Nov 13 '18 at 22:14









Levy MoreiraLevy Moreira

5782510




5782510












  • Thanks Levy. Now it is working fine!!!

    – Neto
    Nov 13 '18 at 22:23

















  • Thanks Levy. Now it is working fine!!!

    – Neto
    Nov 13 '18 at 22:23
















Thanks Levy. Now it is working fine!!!

– Neto
Nov 13 '18 at 22:23





Thanks Levy. Now it is working fine!!!

– Neto
Nov 13 '18 at 22:23

















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.




draft saved


draft discarded














StackExchange.ready(
function ()
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53283804%2fhow-submit-angular-form-with-http-interceptor%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







這個網誌中的熱門文章

Barbados

How to read a connectionString WITH PROVIDER in .NET Core?

Node.js Script on GitHub Pages or Amazon S3