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);
}
);
});


    }
enter image description here



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



enter image description here



Here is what i have for accessing the httpheaders



   let headers: HttpHeaders = response.headers;
if (headers && headers['csrftoken']) {
let token: String = headers.get('csrftoken');
}









share|improve this question
























  • 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















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);
}
);
});


    }
enter image description here



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



enter image description here



Here is what i have for accessing the httpheaders



   let headers: HttpHeaders = response.headers;
if (headers && headers['csrftoken']) {
let token: String = headers.get('csrftoken');
}









share|improve this question
























  • 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













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);
}
);
});


    }
enter image description here



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



enter image description here



Here is what i have for accessing the httpheaders



   let headers: HttpHeaders = response.headers;
if (headers && headers['csrftoken']) {
let token: String = headers.get('csrftoken');
}









share|improve this question















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);
}
);
});


    }
enter image description here



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



enter image description here



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






share|improve this question















share|improve this question













share|improve this question




share|improve this question








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. Use headers.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










  • @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
















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












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');
}





share|improve this answer





















    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%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








    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');
    }





    share|improve this answer

























      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');
      }





      share|improve this answer























        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');
        }





        share|improve this answer












        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');
        }






        share|improve this answer












        share|improve this answer



        share|improve this answer










        answered Nov 19 at 22:19









        AlesD

        2,01828




        2,01828






























            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%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





















































            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

            404 Error Contact Form 7 ajax form submitting

            How to know if a Active Directory user can login interactively

            Refactoring coordinates for Minecraft Pi buildings written in Python