How submit Angular form with Http Interceptor
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
|
show 5 more comments
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
Sure, you have aif
condition but noelse
. 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 returnnext.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 theif
statement. What do you return when you don't have anauthToken
? 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
|
show 5 more comments
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
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
angular interceptor angular-http-interceptors
asked Nov 13 '18 at 14:59
NetoNeto
376
376
Sure, you have aif
condition but noelse
. 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 returnnext.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 theif
statement. What do you return when you don't have anauthToken
? 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
|
show 5 more comments
Sure, you have aif
condition but noelse
. 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 returnnext.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 theif
statement. What do you return when you don't have anauthToken
? 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
|
show 5 more comments
1 Answer
1
active
oldest
votes
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)
Thanks Levy. Now it is working fine!!!
– Neto
Nov 13 '18 at 22:23
add a comment |
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
);
);
Sign up or log in
StackExchange.ready(function ()
StackExchange.helpers.onClickDraftSave('#login-link');
);
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
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
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)
Thanks Levy. Now it is working fine!!!
– Neto
Nov 13 '18 at 22:23
add a comment |
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)
Thanks Levy. Now it is working fine!!!
– Neto
Nov 13 '18 at 22:23
add a comment |
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)
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)
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
add a comment |
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
add a comment |
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.
Sign up or log in
StackExchange.ready(function ()
StackExchange.helpers.onClickDraftSave('#login-link');
);
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
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
Sign up or log in
StackExchange.ready(function ()
StackExchange.helpers.onClickDraftSave('#login-link');
);
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function ()
StackExchange.helpers.onClickDraftSave('#login-link');
);
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function ()
StackExchange.helpers.onClickDraftSave('#login-link');
);
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
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
Sure, you have a
if
condition but noelse
. 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 anauthToken
? 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