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);
}
);
});
}
angular angular-httpclient
add a comment |
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);
}
);
});
}
angular angular-httpclient
2
It's{observe: ... }
, not{observable: ... }
– Ingo Bürk
Nov 19 at 18:10
add a comment |
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);
}
);
});
}
angular angular-httpclient
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
angular angular-httpclient
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
add a comment |
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
add a comment |
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();
}
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
add a comment |
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
add a comment |
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();
}
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
add a comment |
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();
}
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
add a comment |
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();
}
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();
}
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
add a comment |
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
add a comment |
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
add a comment |
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
add a comment |
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
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
answered Nov 19 at 18:15
nkuma_12
39311
39311
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%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
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
2
It's
{observe: ... }
, not{observable: ... }
– Ingo Bürk
Nov 19 at 18:10