angular 5 httpclient , on post , the response does not return headers, only returns response object











up vote
0
down vote

favorite












I recently moved to angular 5 and earlier the http.post was returning the response objecta s complete with headers however using httpclient , it does not return the response headers. How can i change this part of the post code to provide the complete response object including response headers



I read about using {observable: "response"} , but its not working



post(inURL,  data, config = undefined)  {
let headers, options;
if (config) {
options = config;
} else {
//headers = this.getDefaultHeader();
//options = new RequestOptions({ headers: headers });
options = httpOptions;
}

let action = this.httpService.post(inURL,  data, options);
return new Promise((resolve, reject) => {
action.subscribe(
response => resolve(response),
error => {
this.interceptError(error);
reject(error);
}
);
});    
}









share|improve this question




















  • 2




    It's {observe: ... }, not {observable: ... }
    – Ingo Bürk
    Nov 19 at 18:10















up vote
0
down vote

favorite












I recently moved to angular 5 and earlier the http.post was returning the response objecta s complete with headers however using httpclient , it does not return the response headers. How can i change this part of the post code to provide the complete response object including response headers



I read about using {observable: "response"} , but its not working



post(inURL,  data, config = undefined)  {
let headers, options;
if (config) {
options = config;
} else {
//headers = this.getDefaultHeader();
//options = new RequestOptions({ headers: headers });
options = httpOptions;
}

let action = this.httpService.post(inURL,  data, options);
return new Promise((resolve, reject) => {
action.subscribe(
response => resolve(response),
error => {
this.interceptError(error);
reject(error);
}
);
});    
}









share|improve this question




















  • 2




    It's {observe: ... }, not {observable: ... }
    – Ingo Bürk
    Nov 19 at 18:10













up vote
0
down vote

favorite









up vote
0
down vote

favorite











I recently moved to angular 5 and earlier the http.post was returning the response objecta s complete with headers however using httpclient , it does not return the response headers. How can i change this part of the post code to provide the complete response object including response headers



I read about using {observable: "response"} , but its not working



post(inURL,  data, config = undefined)  {
let headers, options;
if (config) {
options = config;
} else {
//headers = this.getDefaultHeader();
//options = new RequestOptions({ headers: headers });
options = httpOptions;
}

let action = this.httpService.post(inURL,  data, options);
return new Promise((resolve, reject) => {
action.subscribe(
response => resolve(response),
error => {
this.interceptError(error);
reject(error);
}
);
});    
}









share|improve this question















I recently moved to angular 5 and earlier the http.post was returning the response objecta s complete with headers however using httpclient , it does not return the response headers. How can i change this part of the post code to provide the complete response object including response headers



I read about using {observable: "response"} , but its not working



post(inURL,  data, config = undefined)  {
let headers, options;
if (config) {
options = config;
} else {
//headers = this.getDefaultHeader();
//options = new RequestOptions({ headers: headers });
options = httpOptions;
}

let action = this.httpService.post(inURL,  data, options);
return new Promise((resolve, reject) => {
action.subscribe(
response => resolve(response),
error => {
this.interceptError(error);
reject(error);
}
);
});    
}






angular angular-httpclient






share|improve this question















share|improve this question













share|improve this question




share|improve this question








edited Nov 19 at 18:05









SiddAjmera

11.2k21137




11.2k21137










asked Nov 19 at 18:02









looneytunes

2911624




2911624








  • 2




    It's {observe: ... }, not {observable: ... }
    – Ingo Bürk
    Nov 19 at 18:10














  • 2




    It's {observe: ... }, not {observable: ... }
    – Ingo Bürk
    Nov 19 at 18:10








2




2




It's {observe: ... }, not {observable: ... }
– Ingo Bürk
Nov 19 at 18:10




It's {observe: ... }, not {observable: ... }
– Ingo Bürk
Nov 19 at 18:10












2 Answers
2






active

oldest

votes

















up vote
1
down vote



accepted










I'm assuming that you have a very strong reason to return a promise instead of an Observable from here.



That being said, you're complicating things here. You could have simply called toPromise() on the return value from post.



And well, you said that you used {observable: "response"} but it didn't work. That's because you didn't use it correctly. It SHOULD BE {observe: "response"} and NOT {observable: "response"}



It must be a part of the options. So I've used the spread operator(...) and added {observe: "response"} to options.



Give this a try:



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

this.httpService.post(inURL, data, options)
.toPromise();
}





share|improve this answer



















  • 1




    You repeated OP's typo everywhere except for in your code.
    – Ingo Bürk
    Nov 19 at 18:11










  • @IngoBürk, copy paste, I guess. I've fixed it now BTW :)
    – SiddAjmera
    Nov 19 at 18:12










  • @SiddAjmera thanks , that works , however i am trying to retrive a custom token from the response of a post , but with this , i still dont see the header object having the key I am looking for ' xxxtoken' which the response header has , however the returned header does not provide access to this xxxtoken key
    – looneytunes
    Nov 19 at 18:37










  • @looneytunes, you might want to check if the API actually returns such a header, probably in Postman.
    – SiddAjmera
    Nov 19 at 18:39










  • yes it does return , cache-control: no-cache, no-store, must-revalidate content-security-policy: default-src 'self';font-src 'self' data:;script-src 'self' 'unsafe-inline' 'unsafe-eval' data:;style-src 'self' 'unsafe-inline';frame-ancestors 'none' content-type: application/json;charset=UTF-8 xxxtoken: e33e8be9-cd14-4114-8bcf-adee60e8fc14 set-cookie: JSESSIONID=CED04E7652F8B31DBAFF7AB041330F33; Path=/view-vlsi; Secure; HttpOnly status: 200 strict-transport-security: max-age=31536000
    – looneytunes
    Nov 19 at 18:41


















up vote
0
down vote













You can just return the HttpClient reponse after making the HTTP call:



getServiceData(): Observable<any>{
return this.httpClient
.post(myUrl, requestBodyObj)
.map((httpResponse: any) => {
if (httpResponse.operationStatus.statusCode === '0') {
return httpResponse.data;
} else {
throw new Error('error in service call');
}
})

}


In your component class you can subscribe to the service method:



myService.subscribe( (data) => {
// do something with data received
}, error => {
// do something when error
})


More details on HttpClient usage can be seen here at official guide






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%2f53380298%2fangular-5-httpclient-on-post-the-response-does-not-return-headers-only-retu%23new-answer', 'question_page');
    }
    );

    Post as a guest















    Required, but never shown

























    2 Answers
    2






    active

    oldest

    votes








    2 Answers
    2






    active

    oldest

    votes









    active

    oldest

    votes






    active

    oldest

    votes








    up vote
    1
    down vote



    accepted










    I'm assuming that you have a very strong reason to return a promise instead of an Observable from here.



    That being said, you're complicating things here. You could have simply called toPromise() on the return value from post.



    And well, you said that you used {observable: "response"} but it didn't work. That's because you didn't use it correctly. It SHOULD BE {observe: "response"} and NOT {observable: "response"}



    It must be a part of the options. So I've used the spread operator(...) and added {observe: "response"} to options.



    Give this a try:



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

    this.httpService.post(inURL, data, options)
    .toPromise();
    }





    share|improve this answer



















    • 1




      You repeated OP's typo everywhere except for in your code.
      – Ingo Bürk
      Nov 19 at 18:11










    • @IngoBürk, copy paste, I guess. I've fixed it now BTW :)
      – SiddAjmera
      Nov 19 at 18:12










    • @SiddAjmera thanks , that works , however i am trying to retrive a custom token from the response of a post , but with this , i still dont see the header object having the key I am looking for ' xxxtoken' which the response header has , however the returned header does not provide access to this xxxtoken key
      – looneytunes
      Nov 19 at 18:37










    • @looneytunes, you might want to check if the API actually returns such a header, probably in Postman.
      – SiddAjmera
      Nov 19 at 18:39










    • yes it does return , cache-control: no-cache, no-store, must-revalidate content-security-policy: default-src 'self';font-src 'self' data:;script-src 'self' 'unsafe-inline' 'unsafe-eval' data:;style-src 'self' 'unsafe-inline';frame-ancestors 'none' content-type: application/json;charset=UTF-8 xxxtoken: e33e8be9-cd14-4114-8bcf-adee60e8fc14 set-cookie: JSESSIONID=CED04E7652F8B31DBAFF7AB041330F33; Path=/view-vlsi; Secure; HttpOnly status: 200 strict-transport-security: max-age=31536000
      – looneytunes
      Nov 19 at 18:41















    up vote
    1
    down vote



    accepted










    I'm assuming that you have a very strong reason to return a promise instead of an Observable from here.



    That being said, you're complicating things here. You could have simply called toPromise() on the return value from post.



    And well, you said that you used {observable: "response"} but it didn't work. That's because you didn't use it correctly. It SHOULD BE {observe: "response"} and NOT {observable: "response"}



    It must be a part of the options. So I've used the spread operator(...) and added {observe: "response"} to options.



    Give this a try:



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

    this.httpService.post(inURL, data, options)
    .toPromise();
    }





    share|improve this answer



















    • 1




      You repeated OP's typo everywhere except for in your code.
      – Ingo Bürk
      Nov 19 at 18:11










    • @IngoBürk, copy paste, I guess. I've fixed it now BTW :)
      – SiddAjmera
      Nov 19 at 18:12










    • @SiddAjmera thanks , that works , however i am trying to retrive a custom token from the response of a post , but with this , i still dont see the header object having the key I am looking for ' xxxtoken' which the response header has , however the returned header does not provide access to this xxxtoken key
      – looneytunes
      Nov 19 at 18:37










    • @looneytunes, you might want to check if the API actually returns such a header, probably in Postman.
      – SiddAjmera
      Nov 19 at 18:39










    • yes it does return , cache-control: no-cache, no-store, must-revalidate content-security-policy: default-src 'self';font-src 'self' data:;script-src 'self' 'unsafe-inline' 'unsafe-eval' data:;style-src 'self' 'unsafe-inline';frame-ancestors 'none' content-type: application/json;charset=UTF-8 xxxtoken: e33e8be9-cd14-4114-8bcf-adee60e8fc14 set-cookie: JSESSIONID=CED04E7652F8B31DBAFF7AB041330F33; Path=/view-vlsi; Secure; HttpOnly status: 200 strict-transport-security: max-age=31536000
      – looneytunes
      Nov 19 at 18:41













    up vote
    1
    down vote



    accepted







    up vote
    1
    down vote



    accepted






    I'm assuming that you have a very strong reason to return a promise instead of an Observable from here.



    That being said, you're complicating things here. You could have simply called toPromise() on the return value from post.



    And well, you said that you used {observable: "response"} but it didn't work. That's because you didn't use it correctly. It SHOULD BE {observe: "response"} and NOT {observable: "response"}



    It must be a part of the options. So I've used the spread operator(...) and added {observe: "response"} to options.



    Give this a try:



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

    this.httpService.post(inURL, data, options)
    .toPromise();
    }





    share|improve this answer














    I'm assuming that you have a very strong reason to return a promise instead of an Observable from here.



    That being said, you're complicating things here. You could have simply called toPromise() on the return value from post.



    And well, you said that you used {observable: "response"} but it didn't work. That's because you didn't use it correctly. It SHOULD BE {observe: "response"} and NOT {observable: "response"}



    It must be a part of the options. So I've used the spread operator(...) and added {observe: "response"} to options.



    Give this a try:



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

    this.httpService.post(inURL, data, options)
    .toPromise();
    }






    share|improve this answer














    share|improve this answer



    share|improve this answer








    edited Nov 19 at 18:28

























    answered Nov 19 at 18:10









    SiddAjmera

    11.2k21137




    11.2k21137








    • 1




      You repeated OP's typo everywhere except for in your code.
      – Ingo Bürk
      Nov 19 at 18:11










    • @IngoBürk, copy paste, I guess. I've fixed it now BTW :)
      – SiddAjmera
      Nov 19 at 18:12










    • @SiddAjmera thanks , that works , however i am trying to retrive a custom token from the response of a post , but with this , i still dont see the header object having the key I am looking for ' xxxtoken' which the response header has , however the returned header does not provide access to this xxxtoken key
      – looneytunes
      Nov 19 at 18:37










    • @looneytunes, you might want to check if the API actually returns such a header, probably in Postman.
      – SiddAjmera
      Nov 19 at 18:39










    • yes it does return , cache-control: no-cache, no-store, must-revalidate content-security-policy: default-src 'self';font-src 'self' data:;script-src 'self' 'unsafe-inline' 'unsafe-eval' data:;style-src 'self' 'unsafe-inline';frame-ancestors 'none' content-type: application/json;charset=UTF-8 xxxtoken: e33e8be9-cd14-4114-8bcf-adee60e8fc14 set-cookie: JSESSIONID=CED04E7652F8B31DBAFF7AB041330F33; Path=/view-vlsi; Secure; HttpOnly status: 200 strict-transport-security: max-age=31536000
      – looneytunes
      Nov 19 at 18:41














    • 1




      You repeated OP's typo everywhere except for in your code.
      – Ingo Bürk
      Nov 19 at 18:11










    • @IngoBürk, copy paste, I guess. I've fixed it now BTW :)
      – SiddAjmera
      Nov 19 at 18:12










    • @SiddAjmera thanks , that works , however i am trying to retrive a custom token from the response of a post , but with this , i still dont see the header object having the key I am looking for ' xxxtoken' which the response header has , however the returned header does not provide access to this xxxtoken key
      – looneytunes
      Nov 19 at 18:37










    • @looneytunes, you might want to check if the API actually returns such a header, probably in Postman.
      – SiddAjmera
      Nov 19 at 18:39










    • yes it does return , cache-control: no-cache, no-store, must-revalidate content-security-policy: default-src 'self';font-src 'self' data:;script-src 'self' 'unsafe-inline' 'unsafe-eval' data:;style-src 'self' 'unsafe-inline';frame-ancestors 'none' content-type: application/json;charset=UTF-8 xxxtoken: e33e8be9-cd14-4114-8bcf-adee60e8fc14 set-cookie: JSESSIONID=CED04E7652F8B31DBAFF7AB041330F33; Path=/view-vlsi; Secure; HttpOnly status: 200 strict-transport-security: max-age=31536000
      – looneytunes
      Nov 19 at 18:41








    1




    1




    You repeated OP's typo everywhere except for in your code.
    – Ingo Bürk
    Nov 19 at 18:11




    You repeated OP's typo everywhere except for in your code.
    – Ingo Bürk
    Nov 19 at 18:11












    @IngoBürk, copy paste, I guess. I've fixed it now BTW :)
    – SiddAjmera
    Nov 19 at 18:12




    @IngoBürk, copy paste, I guess. I've fixed it now BTW :)
    – SiddAjmera
    Nov 19 at 18:12












    @SiddAjmera thanks , that works , however i am trying to retrive a custom token from the response of a post , but with this , i still dont see the header object having the key I am looking for ' xxxtoken' which the response header has , however the returned header does not provide access to this xxxtoken key
    – looneytunes
    Nov 19 at 18:37




    @SiddAjmera thanks , that works , however i am trying to retrive a custom token from the response of a post , but with this , i still dont see the header object having the key I am looking for ' xxxtoken' which the response header has , however the returned header does not provide access to this xxxtoken key
    – looneytunes
    Nov 19 at 18:37












    @looneytunes, you might want to check if the API actually returns such a header, probably in Postman.
    – SiddAjmera
    Nov 19 at 18:39




    @looneytunes, you might want to check if the API actually returns such a header, probably in Postman.
    – SiddAjmera
    Nov 19 at 18:39












    yes it does return , cache-control: no-cache, no-store, must-revalidate content-security-policy: default-src 'self';font-src 'self' data:;script-src 'self' 'unsafe-inline' 'unsafe-eval' data:;style-src 'self' 'unsafe-inline';frame-ancestors 'none' content-type: application/json;charset=UTF-8 xxxtoken: e33e8be9-cd14-4114-8bcf-adee60e8fc14 set-cookie: JSESSIONID=CED04E7652F8B31DBAFF7AB041330F33; Path=/view-vlsi; Secure; HttpOnly status: 200 strict-transport-security: max-age=31536000
    – looneytunes
    Nov 19 at 18:41




    yes it does return , cache-control: no-cache, no-store, must-revalidate content-security-policy: default-src 'self';font-src 'self' data:;script-src 'self' 'unsafe-inline' 'unsafe-eval' data:;style-src 'self' 'unsafe-inline';frame-ancestors 'none' content-type: application/json;charset=UTF-8 xxxtoken: e33e8be9-cd14-4114-8bcf-adee60e8fc14 set-cookie: JSESSIONID=CED04E7652F8B31DBAFF7AB041330F33; Path=/view-vlsi; Secure; HttpOnly status: 200 strict-transport-security: max-age=31536000
    – looneytunes
    Nov 19 at 18:41












    up vote
    0
    down vote













    You can just return the HttpClient reponse after making the HTTP call:



    getServiceData(): Observable<any>{
    return this.httpClient
    .post(myUrl, requestBodyObj)
    .map((httpResponse: any) => {
    if (httpResponse.operationStatus.statusCode === '0') {
    return httpResponse.data;
    } else {
    throw new Error('error in service call');
    }
    })

    }


    In your component class you can subscribe to the service method:



    myService.subscribe( (data) => {
    // do something with data received
    }, error => {
    // do something when error
    })


    More details on HttpClient usage can be seen here at official guide






    share|improve this answer

























      up vote
      0
      down vote













      You can just return the HttpClient reponse after making the HTTP call:



      getServiceData(): Observable<any>{
      return this.httpClient
      .post(myUrl, requestBodyObj)
      .map((httpResponse: any) => {
      if (httpResponse.operationStatus.statusCode === '0') {
      return httpResponse.data;
      } else {
      throw new Error('error in service call');
      }
      })

      }


      In your component class you can subscribe to the service method:



      myService.subscribe( (data) => {
      // do something with data received
      }, error => {
      // do something when error
      })


      More details on HttpClient usage can be seen here at official guide






      share|improve this answer























        up vote
        0
        down vote










        up vote
        0
        down vote









        You can just return the HttpClient reponse after making the HTTP call:



        getServiceData(): Observable<any>{
        return this.httpClient
        .post(myUrl, requestBodyObj)
        .map((httpResponse: any) => {
        if (httpResponse.operationStatus.statusCode === '0') {
        return httpResponse.data;
        } else {
        throw new Error('error in service call');
        }
        })

        }


        In your component class you can subscribe to the service method:



        myService.subscribe( (data) => {
        // do something with data received
        }, error => {
        // do something when error
        })


        More details on HttpClient usage can be seen here at official guide






        share|improve this answer












        You can just return the HttpClient reponse after making the HTTP call:



        getServiceData(): Observable<any>{
        return this.httpClient
        .post(myUrl, requestBodyObj)
        .map((httpResponse: any) => {
        if (httpResponse.operationStatus.statusCode === '0') {
        return httpResponse.data;
        } else {
        throw new Error('error in service call');
        }
        })

        }


        In your component class you can subscribe to the service method:



        myService.subscribe( (data) => {
        // do something with data received
        }, error => {
        // do something when error
        })


        More details on HttpClient usage can be seen here at official guide







        share|improve this answer












        share|improve this answer



        share|improve this answer










        answered Nov 19 at 18:15









        nkuma_12

        39311




        39311






























            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%2f53380298%2fangular-5-httpclient-on-post-the-response-does-not-return-headers-only-retu%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

            TypeError: fit_transform() missing 1 required positional argument: 'X'