{"id":2127,"date":"2022-08-27T22:07:24","date_gmt":"2022-08-27T20:07:24","guid":{"rendered":"https:\/\/nguenkam.com\/blog\/?p=2127"},"modified":"2022-08-27T22:12:59","modified_gmt":"2022-08-27T20:12:59","slug":"how-to-use-reportprogress-in-httpclient-in-angular","status":"publish","type":"post","link":"https:\/\/nguenkam.com\/blog\/index.php\/2022\/08\/27\/how-to-use-reportprogress-in-httpclient-in-angular\/","title":{"rendered":"How to use reportProgress in HttpClient in Angular"},"content":{"rendered":"\n<p>Let\u00b4s assume, we are downloading file using&nbsp;<code>HTTP<\/code>&nbsp;<code>POST<\/code>&nbsp;method. We want to call another method to show download progress to end user until file download complete. How to use&nbsp;<code>reportProgress<\/code>&nbsp;in&nbsp;<code>HttpClient<\/code>&nbsp;for this?<\/p>\n\n\n\n<div style=\"height:100px\" aria-hidden=\"true\" class=\"wp-block-spacer\"><\/div>\n\n\n\n<h4>Solution:<\/h4>\n\n\n\n<ol><li>We need to use\u00a0<strong><code>reportProgress: true<\/code><\/strong>\u00a0to show some progress of any\u00a0<strong><code>HTTP<\/code><\/strong>\u00a0request<\/li><li><span class=\"has-inline-color has-black-color\">If we want to see all events, including the progress of transferts\u00a0,\u00a0<\/span>we need to use\u00a0<strong><code>observe: 'events'<\/code><\/strong>\u00a0option as well and return an\u00a0<strong><code>Observable<\/code><\/strong>\u00a0of type\u00a0<strong><code>HttpEvent<\/code><\/strong>.<\/li><li>Then we can catch all the\u00a0<strong><code>events(DownloadProgress, Response..etc)<\/code><\/strong>\u00a0in the component method.<\/li><\/ol>\n\n\n\n<pre class=\"wp-block-code\"><code> downfile(file: any): Observable&lt;HttpEvent&lt;any>>{\n\n    const options = {\n        responseType: 'blob' as const,\n        reportProgress: true, \n        observe: \"events\" as const,\n        headers: new HttpHeaders({ 'Content-Type': 'application\/json' })\n    };\n  \n    return this.http.post(this.url , app, options);\n  }<\/code><\/pre>\n\n\n\n<p><\/p>\n\n\n\n<p><strong>Then in component we can catch all the&nbsp;<code>events<\/code>&nbsp;as below.<\/strong><\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>this.myService.downfile(file)\n    .subscribe(event =&gt; {\n\n        if (event.type === HttpEventType.DownloadProgress) {\n\n           const percentDone = Math.round(100 * event.loaded \/ event.total);\n            console.log(\"download in progress ====&gt; \" + percentDone + \"%\");\n        }\n\n        if (event.type === HttpEventType.Response) {\n            console.log(\"donwload completed\");\n        }\n});<\/code><\/pre>\n\n\n\n<div style=\"height:100px\" aria-hidden=\"true\" class=\"wp-block-spacer\"><\/div>\n\n\n\n<p><strong>PS-1<\/strong>: <em>&nbsp;there are different kinds of&nbsp;<code><a href=\"https:\/\/angular.io\/api\/common\/http\/HttpEvent\">HttpEvent<\/a><\/code>.<\/em><\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>enum HttpEventType {\n  Sent\n  UploadProgress\n  ResponseHeader\n  DownloadProgress\n  Response\n  User\n}<\/code><\/pre>\n\n\n\n<p><strong>PS-2 :<\/strong> (<em><code>OBSERVE<\/code>&nbsp;AND&nbsp;<code>RESPONSE<\/code>&nbsp;TYPES) ==&gt;<\/em> <em>The types of the&nbsp;<code>observe<\/code>&nbsp;and&nbsp;<code>response<\/code>&nbsp;options are&nbsp;string unions, rather than plain strings.<\/em><\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>options: {\n  \u2026\n  observe?: 'body' | 'events' | 'response',\n  \u2026\n  responseType?: 'arraybuffer'|'blob'|'json'|'text',\n  \u2026\n}<\/code><\/pre>\n\n\n\n<p>This can cause confusion. For example:<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>\/\/ this works\nclient.get('\/foo', {responseType: 'text'})\n\n\/\/ but this does NOT work\nconst options = {\n  responseType: 'text',\n};\nclient.get('\/foo', options)<\/code><\/pre>\n\n\n\n<p>In the second case, TypeScript infers the type of&nbsp;<code>options<\/code>&nbsp;to be&nbsp;<code>{responseType: string}<\/code>. The type is too wide to pass to&nbsp;<code>HttpClient.get<\/code>&nbsp;which is expecting the type of&nbsp;<code>responseType<\/code>&nbsp;to be one of the&nbsp;<em>specific<\/em>&nbsp;strings.&nbsp;<code><a href=\"https:\/\/angular.io\/api\/common\/http\/HttpClient\">HttpClient<\/a><\/code>&nbsp;is typed explicitly this way so that the compiler can report the correct return type based on the options you provided.<\/p>\n\n\n\n<p>Use&nbsp;<code>as const<\/code>&nbsp;to let TypeScript know that you really do mean to use a constant string type:<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>const options = {\n  responseType: 'text' as const,\n};\nclient.get('\/foo', options);<\/code><\/pre>\n","protected":false},"excerpt":{"rendered":"<p>Let\u00b4s assume, we are downloading file using&nbsp;HTTP&nbsp;POST&nbsp;method. We want to call another method to show download progress to end user until file download complete. How to use&nbsp;reportProgress&nbsp;in&nbsp;HttpClient&nbsp;for this? Solution: We need to use\u00a0reportProgress: true\u00a0to show some progress of any\u00a0HTTP\u00a0request If we want to see all events, including the progress of transferts\u00a0,\u00a0we need to use\u00a0observe: &#8216;events&#8217;\u00a0option [&hellip;]<\/p>\n","protected":false},"author":1,"featured_media":1965,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":[],"categories":[37],"tags":[585,326,528,587,584,161,217,586],"_links":{"self":[{"href":"https:\/\/nguenkam.com\/blog\/index.php\/wp-json\/wp\/v2\/posts\/2127"}],"collection":[{"href":"https:\/\/nguenkam.com\/blog\/index.php\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/nguenkam.com\/blog\/index.php\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/nguenkam.com\/blog\/index.php\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/nguenkam.com\/blog\/index.php\/wp-json\/wp\/v2\/comments?post=2127"}],"version-history":[{"count":6,"href":"https:\/\/nguenkam.com\/blog\/index.php\/wp-json\/wp\/v2\/posts\/2127\/revisions"}],"predecessor-version":[{"id":2133,"href":"https:\/\/nguenkam.com\/blog\/index.php\/wp-json\/wp\/v2\/posts\/2127\/revisions\/2133"}],"wp:featuredmedia":[{"embeddable":true,"href":"https:\/\/nguenkam.com\/blog\/index.php\/wp-json\/wp\/v2\/media\/1965"}],"wp:attachment":[{"href":"https:\/\/nguenkam.com\/blog\/index.php\/wp-json\/wp\/v2\/media?parent=2127"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/nguenkam.com\/blog\/index.php\/wp-json\/wp\/v2\/categories?post=2127"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/nguenkam.com\/blog\/index.php\/wp-json\/wp\/v2\/tags?post=2127"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}