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