How can I disable 'sent' event.type when using a custom http request in Angular?
When I use a not-pre-defined http request in Angular (7), I got response 2 times in my subscribe body, but if I use a pre-define http request like GET, there is only 1 response.
A custom request (SEARCH) in service:
searchFileByCategory(categories: Array<string>): Observable<any> {
return this.http.request(new HttpRequest('SEARCH', ENDPOINTS.SEARCH_FILE_BY_CATEGORY, {categories: categories}));
}
In component:
refreshTable(): void {
this.backEndService.searchFileByCategory(this.categories).subscribe(event => {
console.log(event);
if (event.type === HttpEventType.Response) {
this.files = event.body.data;
}
});
}
1st event log:
{type: 0}
2nd event log:
{body: {data: {…}}
headers: HttpHeaders {normalizedNames: Map(0), lazyUpdate: null, lazyInit: ƒ}
ok: true
status: 200
statusText: "OK"
type: 4
url: "http://localhost/..."}
As you can see I have to check (event.type === HttpEventType.Response) because event.body.data doesn't exist in the 1st response.
HttpEventType enum in node_modules@angularcommonhttpsrcresponse.d.ts:
export declare enum HttpEventType {
/**
* The request was sent out over the wire.
*/
Sent = 0,
/**
* An upload progress event was received.
*/
UploadProgress = 1,
/**
* The response status code and headers were received.
*/
ResponseHeader = 2,
/**
* A download progress event was received.
*/
DownloadProgress = 3,
/**
* The full response including the body was received.
*/
Response = 4,
/**
* A custom event from an interceptor or a backend.
*/
User = 5
}
I have understood what the 1st response is. But I don't understand the reason why GET does not have the same.
A GET request in service:
getZipCodes(): Observable<any> {
return this.http.get(ENDPOINTS.GET_ZIP_CODES);
}
In component:
refreshZipCodes(): void {
this.backEndService.getZipCodes().subscribe(response => this.zipCodes = response.data)
}
Here there is no need for checking (event.type === HttpEventType.Response). Why?
My final purpose is to get rid of checking (event.type === HttpEventType.Response) every time when I call a custom http request. How can I do it?
angular httprequest
add a comment |
When I use a not-pre-defined http request in Angular (7), I got response 2 times in my subscribe body, but if I use a pre-define http request like GET, there is only 1 response.
A custom request (SEARCH) in service:
searchFileByCategory(categories: Array<string>): Observable<any> {
return this.http.request(new HttpRequest('SEARCH', ENDPOINTS.SEARCH_FILE_BY_CATEGORY, {categories: categories}));
}
In component:
refreshTable(): void {
this.backEndService.searchFileByCategory(this.categories).subscribe(event => {
console.log(event);
if (event.type === HttpEventType.Response) {
this.files = event.body.data;
}
});
}
1st event log:
{type: 0}
2nd event log:
{body: {data: {…}}
headers: HttpHeaders {normalizedNames: Map(0), lazyUpdate: null, lazyInit: ƒ}
ok: true
status: 200
statusText: "OK"
type: 4
url: "http://localhost/..."}
As you can see I have to check (event.type === HttpEventType.Response) because event.body.data doesn't exist in the 1st response.
HttpEventType enum in node_modules@angularcommonhttpsrcresponse.d.ts:
export declare enum HttpEventType {
/**
* The request was sent out over the wire.
*/
Sent = 0,
/**
* An upload progress event was received.
*/
UploadProgress = 1,
/**
* The response status code and headers were received.
*/
ResponseHeader = 2,
/**
* A download progress event was received.
*/
DownloadProgress = 3,
/**
* The full response including the body was received.
*/
Response = 4,
/**
* A custom event from an interceptor or a backend.
*/
User = 5
}
I have understood what the 1st response is. But I don't understand the reason why GET does not have the same.
A GET request in service:
getZipCodes(): Observable<any> {
return this.http.get(ENDPOINTS.GET_ZIP_CODES);
}
In component:
refreshZipCodes(): void {
this.backEndService.getZipCodes().subscribe(response => this.zipCodes = response.data)
}
Here there is no need for checking (event.type === HttpEventType.Response). Why?
My final purpose is to get rid of checking (event.type === HttpEventType.Response) every time when I call a custom http request. How can I do it?
angular httprequest
from angular sourceevents$.filter((event: HttpEvent<any>) => event instanceof HttpResponse));
, so you can do the same filtering to to remove type=0 event
– ABOS
Dec 30 '18 at 17:39
Is there a reason you are using a custom get request.
– Ankesh
Dec 30 '18 at 17:39
@Ankesh One of the reasons: GET does not have body and the criterias of the searching procedure is more complex than query params can provide.
– Pa Ri
Dec 30 '18 at 17:50
@ABOS you were right: it works.
– Pa Ri
Dec 30 '18 at 18:04
@ABOS thanks.................:)
– Pa Ri
Dec 30 '18 at 18:12
add a comment |
When I use a not-pre-defined http request in Angular (7), I got response 2 times in my subscribe body, but if I use a pre-define http request like GET, there is only 1 response.
A custom request (SEARCH) in service:
searchFileByCategory(categories: Array<string>): Observable<any> {
return this.http.request(new HttpRequest('SEARCH', ENDPOINTS.SEARCH_FILE_BY_CATEGORY, {categories: categories}));
}
In component:
refreshTable(): void {
this.backEndService.searchFileByCategory(this.categories).subscribe(event => {
console.log(event);
if (event.type === HttpEventType.Response) {
this.files = event.body.data;
}
});
}
1st event log:
{type: 0}
2nd event log:
{body: {data: {…}}
headers: HttpHeaders {normalizedNames: Map(0), lazyUpdate: null, lazyInit: ƒ}
ok: true
status: 200
statusText: "OK"
type: 4
url: "http://localhost/..."}
As you can see I have to check (event.type === HttpEventType.Response) because event.body.data doesn't exist in the 1st response.
HttpEventType enum in node_modules@angularcommonhttpsrcresponse.d.ts:
export declare enum HttpEventType {
/**
* The request was sent out over the wire.
*/
Sent = 0,
/**
* An upload progress event was received.
*/
UploadProgress = 1,
/**
* The response status code and headers were received.
*/
ResponseHeader = 2,
/**
* A download progress event was received.
*/
DownloadProgress = 3,
/**
* The full response including the body was received.
*/
Response = 4,
/**
* A custom event from an interceptor or a backend.
*/
User = 5
}
I have understood what the 1st response is. But I don't understand the reason why GET does not have the same.
A GET request in service:
getZipCodes(): Observable<any> {
return this.http.get(ENDPOINTS.GET_ZIP_CODES);
}
In component:
refreshZipCodes(): void {
this.backEndService.getZipCodes().subscribe(response => this.zipCodes = response.data)
}
Here there is no need for checking (event.type === HttpEventType.Response). Why?
My final purpose is to get rid of checking (event.type === HttpEventType.Response) every time when I call a custom http request. How can I do it?
angular httprequest
When I use a not-pre-defined http request in Angular (7), I got response 2 times in my subscribe body, but if I use a pre-define http request like GET, there is only 1 response.
A custom request (SEARCH) in service:
searchFileByCategory(categories: Array<string>): Observable<any> {
return this.http.request(new HttpRequest('SEARCH', ENDPOINTS.SEARCH_FILE_BY_CATEGORY, {categories: categories}));
}
In component:
refreshTable(): void {
this.backEndService.searchFileByCategory(this.categories).subscribe(event => {
console.log(event);
if (event.type === HttpEventType.Response) {
this.files = event.body.data;
}
});
}
1st event log:
{type: 0}
2nd event log:
{body: {data: {…}}
headers: HttpHeaders {normalizedNames: Map(0), lazyUpdate: null, lazyInit: ƒ}
ok: true
status: 200
statusText: "OK"
type: 4
url: "http://localhost/..."}
As you can see I have to check (event.type === HttpEventType.Response) because event.body.data doesn't exist in the 1st response.
HttpEventType enum in node_modules@angularcommonhttpsrcresponse.d.ts:
export declare enum HttpEventType {
/**
* The request was sent out over the wire.
*/
Sent = 0,
/**
* An upload progress event was received.
*/
UploadProgress = 1,
/**
* The response status code and headers were received.
*/
ResponseHeader = 2,
/**
* A download progress event was received.
*/
DownloadProgress = 3,
/**
* The full response including the body was received.
*/
Response = 4,
/**
* A custom event from an interceptor or a backend.
*/
User = 5
}
I have understood what the 1st response is. But I don't understand the reason why GET does not have the same.
A GET request in service:
getZipCodes(): Observable<any> {
return this.http.get(ENDPOINTS.GET_ZIP_CODES);
}
In component:
refreshZipCodes(): void {
this.backEndService.getZipCodes().subscribe(response => this.zipCodes = response.data)
}
Here there is no need for checking (event.type === HttpEventType.Response). Why?
My final purpose is to get rid of checking (event.type === HttpEventType.Response) every time when I call a custom http request. How can I do it?
angular httprequest
angular httprequest
asked Dec 30 '18 at 17:27
Pa RiPa Ri
245
245
from angular sourceevents$.filter((event: HttpEvent<any>) => event instanceof HttpResponse));
, so you can do the same filtering to to remove type=0 event
– ABOS
Dec 30 '18 at 17:39
Is there a reason you are using a custom get request.
– Ankesh
Dec 30 '18 at 17:39
@Ankesh One of the reasons: GET does not have body and the criterias of the searching procedure is more complex than query params can provide.
– Pa Ri
Dec 30 '18 at 17:50
@ABOS you were right: it works.
– Pa Ri
Dec 30 '18 at 18:04
@ABOS thanks.................:)
– Pa Ri
Dec 30 '18 at 18:12
add a comment |
from angular sourceevents$.filter((event: HttpEvent<any>) => event instanceof HttpResponse));
, so you can do the same filtering to to remove type=0 event
– ABOS
Dec 30 '18 at 17:39
Is there a reason you are using a custom get request.
– Ankesh
Dec 30 '18 at 17:39
@Ankesh One of the reasons: GET does not have body and the criterias of the searching procedure is more complex than query params can provide.
– Pa Ri
Dec 30 '18 at 17:50
@ABOS you were right: it works.
– Pa Ri
Dec 30 '18 at 18:04
@ABOS thanks.................:)
– Pa Ri
Dec 30 '18 at 18:12
from angular source
events$.filter((event: HttpEvent<any>) => event instanceof HttpResponse));
, so you can do the same filtering to to remove type=0 event– ABOS
Dec 30 '18 at 17:39
from angular source
events$.filter((event: HttpEvent<any>) => event instanceof HttpResponse));
, so you can do the same filtering to to remove type=0 event– ABOS
Dec 30 '18 at 17:39
Is there a reason you are using a custom get request.
– Ankesh
Dec 30 '18 at 17:39
Is there a reason you are using a custom get request.
– Ankesh
Dec 30 '18 at 17:39
@Ankesh One of the reasons: GET does not have body and the criterias of the searching procedure is more complex than query params can provide.
– Pa Ri
Dec 30 '18 at 17:50
@Ankesh One of the reasons: GET does not have body and the criterias of the searching procedure is more complex than query params can provide.
– Pa Ri
Dec 30 '18 at 17:50
@ABOS you were right: it works.
– Pa Ri
Dec 30 '18 at 18:04
@ABOS you were right: it works.
– Pa Ri
Dec 30 '18 at 18:04
@ABOS thanks.................:)
– Pa Ri
Dec 30 '18 at 18:12
@ABOS thanks.................:)
– Pa Ri
Dec 30 '18 at 18:12
add a comment |
1 Answer
1
active
oldest
votes
@ABOS's answer under the post looks good, it works:
searchLastContentByCategory(category: string): Observable<any> {
return this.http.request(new HttpRequest('SEARCH', ENDPOINTS.SEARCH_LAST_CONTENT_BY_CATEGORY, {category: category}))
.pipe(filter((event: HttpEvent<any>) => event instanceof HttpResponse));
}
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%2f53979845%2fhow-can-i-disable-sent-event-type-when-using-a-custom-http-request-in-angular%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
@ABOS's answer under the post looks good, it works:
searchLastContentByCategory(category: string): Observable<any> {
return this.http.request(new HttpRequest('SEARCH', ENDPOINTS.SEARCH_LAST_CONTENT_BY_CATEGORY, {category: category}))
.pipe(filter((event: HttpEvent<any>) => event instanceof HttpResponse));
}
add a comment |
@ABOS's answer under the post looks good, it works:
searchLastContentByCategory(category: string): Observable<any> {
return this.http.request(new HttpRequest('SEARCH', ENDPOINTS.SEARCH_LAST_CONTENT_BY_CATEGORY, {category: category}))
.pipe(filter((event: HttpEvent<any>) => event instanceof HttpResponse));
}
add a comment |
@ABOS's answer under the post looks good, it works:
searchLastContentByCategory(category: string): Observable<any> {
return this.http.request(new HttpRequest('SEARCH', ENDPOINTS.SEARCH_LAST_CONTENT_BY_CATEGORY, {category: category}))
.pipe(filter((event: HttpEvent<any>) => event instanceof HttpResponse));
}
@ABOS's answer under the post looks good, it works:
searchLastContentByCategory(category: string): Observable<any> {
return this.http.request(new HttpRequest('SEARCH', ENDPOINTS.SEARCH_LAST_CONTENT_BY_CATEGORY, {category: category}))
.pipe(filter((event: HttpEvent<any>) => event instanceof HttpResponse));
}
answered Dec 30 '18 at 18:12
Pa RiPa Ri
245
245
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%2f53979845%2fhow-can-i-disable-sent-event-type-when-using-a-custom-http-request-in-angular%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
from angular source
events$.filter((event: HttpEvent<any>) => event instanceof HttpResponse));
, so you can do the same filtering to to remove type=0 event– ABOS
Dec 30 '18 at 17:39
Is there a reason you are using a custom get request.
– Ankesh
Dec 30 '18 at 17:39
@Ankesh One of the reasons: GET does not have body and the criterias of the searching procedure is more complex than query params can provide.
– Pa Ri
Dec 30 '18 at 17:50
@ABOS you were right: it works.
– Pa Ri
Dec 30 '18 at 18:04
@ABOS thanks.................:)
– Pa Ri
Dec 30 '18 at 18:12