angular 5 httpclient , on post , the response does not return custom headers, only returns response object
up vote
1
down vote
favorite
I'm not getting the actual http headers when making the http post call using Angular 5 common httpClient. I'm passing observe: 'response' in request to get the full response (by default httpClient returns only the body in response).
I want to read the csrftoken value from http header response. I can see that csrftoken is available in response headers using chrome browser network view.
But the same is not available when I read the httpClient response in Angular.
post(inURL, data, config = undefined) {
let headers, options;
if (config){
options = config;
} else {
//headers = this.getDefaultHeader();
//options = new RequestOptions({ headers: headers });
options = httpOptions;
}
options = { ...options, observe: 'response' };
let action = this.httpService.post(inURL, data, options);
return new Promise((resolve, reject) => {
action.subscribe(
response => resolve(response),
error => {
this.interceptError(error);
reject(error);
}
);
});
}
and the response headers object looks like this while debugging on chrome debugger, as you see the csrftoken is not available as a key on the response object
Here is what i have for accessing the httpheaders
let headers: HttpHeaders = response.headers;
if (headers && headers['csrftoken']) {
let token: String = headers.get('csrftoken');
}
angular angular-httpclient custom-headers csrf-token
add a comment |
up vote
1
down vote
favorite
I'm not getting the actual http headers when making the http post call using Angular 5 common httpClient. I'm passing observe: 'response' in request to get the full response (by default httpClient returns only the body in response).
I want to read the csrftoken value from http header response. I can see that csrftoken is available in response headers using chrome browser network view.
But the same is not available when I read the httpClient response in Angular.
post(inURL, data, config = undefined) {
let headers, options;
if (config){
options = config;
} else {
//headers = this.getDefaultHeader();
//options = new RequestOptions({ headers: headers });
options = httpOptions;
}
options = { ...options, observe: 'response' };
let action = this.httpService.post(inURL, data, options);
return new Promise((resolve, reject) => {
action.subscribe(
response => resolve(response),
error => {
this.interceptError(error);
reject(error);
}
);
});
}
and the response headers object looks like this while debugging on chrome debugger, as you see the csrftoken is not available as a key on the response object
Here is what i have for accessing the httpheaders
let headers: HttpHeaders = response.headers;
if (headers && headers['csrftoken']) {
let token: String = headers.get('csrftoken');
}
angular angular-httpclient custom-headers csrf-token
Have you at least tried to get the token out of the HttpHeaders object? Show that code.
– JB Nizet
Nov 19 at 21:18
@JBNizet added code to access teh httpheadersobject and updated the post
– looneytunes
Nov 19 at 21:35
1
Here's the documentation of HttpHeaders: angular.io/api/common/http/HttpHeaders. It's not a POJO. Useheaders.has('csrftoken')
.
– JB Nizet
Nov 19 at 21:38
1
awesome , that worked , thanks @JBNizet
– looneytunes
Nov 19 at 22:33
add a comment |
up vote
1
down vote
favorite
up vote
1
down vote
favorite
I'm not getting the actual http headers when making the http post call using Angular 5 common httpClient. I'm passing observe: 'response' in request to get the full response (by default httpClient returns only the body in response).
I want to read the csrftoken value from http header response. I can see that csrftoken is available in response headers using chrome browser network view.
But the same is not available when I read the httpClient response in Angular.
post(inURL, data, config = undefined) {
let headers, options;
if (config){
options = config;
} else {
//headers = this.getDefaultHeader();
//options = new RequestOptions({ headers: headers });
options = httpOptions;
}
options = { ...options, observe: 'response' };
let action = this.httpService.post(inURL, data, options);
return new Promise((resolve, reject) => {
action.subscribe(
response => resolve(response),
error => {
this.interceptError(error);
reject(error);
}
);
});
}
and the response headers object looks like this while debugging on chrome debugger, as you see the csrftoken is not available as a key on the response object
Here is what i have for accessing the httpheaders
let headers: HttpHeaders = response.headers;
if (headers && headers['csrftoken']) {
let token: String = headers.get('csrftoken');
}
angular angular-httpclient custom-headers csrf-token
I'm not getting the actual http headers when making the http post call using Angular 5 common httpClient. I'm passing observe: 'response' in request to get the full response (by default httpClient returns only the body in response).
I want to read the csrftoken value from http header response. I can see that csrftoken is available in response headers using chrome browser network view.
But the same is not available when I read the httpClient response in Angular.
post(inURL, data, config = undefined) {
let headers, options;
if (config){
options = config;
} else {
//headers = this.getDefaultHeader();
//options = new RequestOptions({ headers: headers });
options = httpOptions;
}
options = { ...options, observe: 'response' };
let action = this.httpService.post(inURL, data, options);
return new Promise((resolve, reject) => {
action.subscribe(
response => resolve(response),
error => {
this.interceptError(error);
reject(error);
}
);
});
}
and the response headers object looks like this while debugging on chrome debugger, as you see the csrftoken is not available as a key on the response object
Here is what i have for accessing the httpheaders
let headers: HttpHeaders = response.headers;
if (headers && headers['csrftoken']) {
let token: String = headers.get('csrftoken');
}
angular angular-httpclient custom-headers csrf-token
angular angular-httpclient custom-headers csrf-token
edited Nov 19 at 21:35
asked Nov 19 at 20:34
looneytunes
2961624
2961624
Have you at least tried to get the token out of the HttpHeaders object? Show that code.
– JB Nizet
Nov 19 at 21:18
@JBNizet added code to access teh httpheadersobject and updated the post
– looneytunes
Nov 19 at 21:35
1
Here's the documentation of HttpHeaders: angular.io/api/common/http/HttpHeaders. It's not a POJO. Useheaders.has('csrftoken')
.
– JB Nizet
Nov 19 at 21:38
1
awesome , that worked , thanks @JBNizet
– looneytunes
Nov 19 at 22:33
add a comment |
Have you at least tried to get the token out of the HttpHeaders object? Show that code.
– JB Nizet
Nov 19 at 21:18
@JBNizet added code to access teh httpheadersobject and updated the post
– looneytunes
Nov 19 at 21:35
1
Here's the documentation of HttpHeaders: angular.io/api/common/http/HttpHeaders. It's not a POJO. Useheaders.has('csrftoken')
.
– JB Nizet
Nov 19 at 21:38
1
awesome , that worked , thanks @JBNizet
– looneytunes
Nov 19 at 22:33
Have you at least tried to get the token out of the HttpHeaders object? Show that code.
– JB Nizet
Nov 19 at 21:18
Have you at least tried to get the token out of the HttpHeaders object? Show that code.
– JB Nizet
Nov 19 at 21:18
@JBNizet added code to access teh httpheadersobject and updated the post
– looneytunes
Nov 19 at 21:35
@JBNizet added code to access teh httpheadersobject and updated the post
– looneytunes
Nov 19 at 21:35
1
1
Here's the documentation of HttpHeaders: angular.io/api/common/http/HttpHeaders. It's not a POJO. Use
headers.has('csrftoken')
.– JB Nizet
Nov 19 at 21:38
Here's the documentation of HttpHeaders: angular.io/api/common/http/HttpHeaders. It's not a POJO. Use
headers.has('csrftoken')
.– JB Nizet
Nov 19 at 21:38
1
1
awesome , that worked , thanks @JBNizet
– looneytunes
Nov 19 at 22:33
awesome , that worked , thanks @JBNizet
– looneytunes
Nov 19 at 22:33
add a comment |
1 Answer
1
active
oldest
votes
up vote
3
down vote
accepted
As already suggested in the comments you cannot check if a header exists the way you are doing it. You need to use the has method. Your code should look like this:
let headers: HttpHeaders = response.headers;
if (headers && headers.has('csrftoken')) {
let token: String = headers.get('csrftoken');
}
add a comment |
1 Answer
1
active
oldest
votes
1 Answer
1
active
oldest
votes
active
oldest
votes
active
oldest
votes
up vote
3
down vote
accepted
As already suggested in the comments you cannot check if a header exists the way you are doing it. You need to use the has method. Your code should look like this:
let headers: HttpHeaders = response.headers;
if (headers && headers.has('csrftoken')) {
let token: String = headers.get('csrftoken');
}
add a comment |
up vote
3
down vote
accepted
As already suggested in the comments you cannot check if a header exists the way you are doing it. You need to use the has method. Your code should look like this:
let headers: HttpHeaders = response.headers;
if (headers && headers.has('csrftoken')) {
let token: String = headers.get('csrftoken');
}
add a comment |
up vote
3
down vote
accepted
up vote
3
down vote
accepted
As already suggested in the comments you cannot check if a header exists the way you are doing it. You need to use the has method. Your code should look like this:
let headers: HttpHeaders = response.headers;
if (headers && headers.has('csrftoken')) {
let token: String = headers.get('csrftoken');
}
As already suggested in the comments you cannot check if a header exists the way you are doing it. You need to use the has method. Your code should look like this:
let headers: HttpHeaders = response.headers;
if (headers && headers.has('csrftoken')) {
let token: String = headers.get('csrftoken');
}
answered Nov 19 at 22:19
AlesD
2,01828
2,01828
add a comment |
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.
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.
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%2f53382231%2fangular-5-httpclient-on-post-the-response-does-not-return-custom-headers-on%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
Have you at least tried to get the token out of the HttpHeaders object? Show that code.
– JB Nizet
Nov 19 at 21:18
@JBNizet added code to access teh httpheadersobject and updated the post
– looneytunes
Nov 19 at 21:35
1
Here's the documentation of HttpHeaders: angular.io/api/common/http/HttpHeaders. It's not a POJO. Use
headers.has('csrftoken')
.– JB Nizet
Nov 19 at 21:38
1
awesome , that worked , thanks @JBNizet
– looneytunes
Nov 19 at 22:33