angular 5 httpclient , on post , the response does not return custom headers, only returns response object
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 |
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 '18 at 21:18
@JBNizet added code to access teh httpheadersobject and updated the post
– looneytunes
Nov 19 '18 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 '18 at 21:38
1
awesome , that worked , thanks @JBNizet
– looneytunes
Nov 19 '18 at 22:33
add a comment |
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 '18 at 21:35
looneytunes
asked Nov 19 '18 at 20:34
looneytuneslooneytunes
2961624
2961624
Have you at least tried to get the token out of the HttpHeaders object? Show that code.
– JB Nizet
Nov 19 '18 at 21:18
@JBNizet added code to access teh httpheadersobject and updated the post
– looneytunes
Nov 19 '18 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 '18 at 21:38
1
awesome , that worked , thanks @JBNizet
– looneytunes
Nov 19 '18 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 '18 at 21:18
@JBNizet added code to access teh httpheadersobject and updated the post
– looneytunes
Nov 19 '18 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 '18 at 21:38
1
awesome , that worked , thanks @JBNizet
– looneytunes
Nov 19 '18 at 22:33
Have you at least tried to get the token out of the HttpHeaders object? Show that code.
– JB Nizet
Nov 19 '18 at 21:18
Have you at least tried to get the token out of the HttpHeaders object? Show that code.
– JB Nizet
Nov 19 '18 at 21:18
@JBNizet added code to access teh httpheadersobject and updated the post
– looneytunes
Nov 19 '18 at 21:35
@JBNizet added code to access teh httpheadersobject and updated the post
– looneytunes
Nov 19 '18 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 '18 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 '18 at 21:38
1
1
awesome , that worked , thanks @JBNizet
– looneytunes
Nov 19 '18 at 22:33
awesome , that worked , thanks @JBNizet
– looneytunes
Nov 19 '18 at 22:33
add a comment |
1 Answer
1
active
oldest
votes
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 |
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%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
1 Answer
1
active
oldest
votes
1 Answer
1
active
oldest
votes
active
oldest
votes
active
oldest
votes
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 |
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 |
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 '18 at 22:19
AlesDAlesD
2,37329
2,37329
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.
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 '18 at 21:18
@JBNizet added code to access teh httpheadersobject and updated the post
– looneytunes
Nov 19 '18 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 '18 at 21:38
1
awesome , that worked , thanks @JBNizet
– looneytunes
Nov 19 '18 at 22:33