{"id":169,"date":"2020-12-07T05:08:04","date_gmt":"2020-12-07T05:08:04","guid":{"rendered":"http:\/\/santosh-shah.com\/?p=169"},"modified":"2021-05-19T06:59:57","modified_gmt":"2021-05-19T06:59:57","slug":"how-to-modify-the-response-to-an-http-request-and-access-it-before-return-it-out-from-observable","status":"publish","type":"post","link":"https:\/\/santosh-shah.com\/blog\/how-to-modify-the-response-to-an-http-request-and-access-it-before-return-it-out-from-observable\/","title":{"rendered":"How to modify the response to an HTTP request and access it before return it out from Observable?"},"content":{"rendered":"\n<p>We often need to modify the response that is return from the http request before it return as observable. That means before we parse it into our component we might need to update. <\/p>\n\n\n\n<p>It all depends on the version of RxJs. Angular 6 launches with RxJs 6 &#8211; which means that the <code>map()\/catch()<\/code> approach is no longer valid.<\/p>\n\n\n\n<p>Instead, you have to use <code>pipe + map()\/catchError() <\/code>as shown below:<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>return this.http.get&lt;SomeType&gt;(url, {headers: this.headers})\n  .pipe(\n     map( response =&gt; {  \/\/ NOTE: response is of type SomeType\n         \/\/ Does something on response.data\n         \/\/ modify the response.data as you see fit.\n\n         \/\/ return the modified data:\n         return response; \/\/ kind of useless\n     }),\n     catchError( error =&gt; {\n         return throwError(error); \/\/ From 'rxjs'\n     })\n  ); \/\/ end of pipe<\/code><\/pre>\n\n\n\n<p>Again with this asynchronous request we will see how we can update data before subscribe.<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>this._app.getApp(id).pipe(map(obj=&gt;obj.map(x=&gt; ({ ...x, isExpanded: false }))) , takeUntil(this.destroy$)).subscribe((res)=&gt;{\n      console.log('----&gt;&gt;&gt;', res);\n    });\n  }<\/code><\/pre>\n","protected":false},"excerpt":{"rendered":"<p>We often need to modify the response that is return from the http request before it return as observable. That means before we parse it into our component we might need to update. It all depends on the version of RxJs. Angular 6 launches with RxJs 6 &#8211; which means that the map()\/catch() approach is [&hellip;]<\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[8],"tags":[],"class_list":["post-169","post","type-post","status-publish","format-standard","hentry","category-angular"],"yoast_head":"<!-- This site is optimized with the Yoast SEO plugin v24.9 - https:\/\/yoast.com\/wordpress\/plugins\/seo\/ -->\n<title>How to modify the response to an HTTP request and access it before return it out from Observable? - Santosh Kumar Shah<\/title>\n<meta name=\"robots\" content=\"index, follow, max-snippet:-1, max-image-preview:large, max-video-preview:-1\" \/>\n<link rel=\"canonical\" href=\"https:\/\/santosh-shah.com\/blog\/how-to-modify-the-response-to-an-http-request-and-access-it-before-return-it-out-from-observable\/\" \/>\n<meta property=\"og:locale\" content=\"en_US\" \/>\n<meta property=\"og:type\" content=\"article\" \/>\n<meta property=\"og:title\" content=\"How to modify the response to an HTTP request and access it before return it out from Observable? - Santosh Kumar Shah\" \/>\n<meta property=\"og:description\" content=\"We often need to modify the response that is return from the http request before it return as observable. That means before we parse it into our component we might need to update. It all depends on the version of RxJs. Angular 6 launches with RxJs 6 &#8211; which means that the map()\/catch() approach is [&hellip;]\" \/>\n<meta property=\"og:url\" content=\"https:\/\/santosh-shah.com\/blog\/how-to-modify-the-response-to-an-http-request-and-access-it-before-return-it-out-from-observable\/\" \/>\n<meta property=\"og:site_name\" content=\"Santosh Kumar Shah\" \/>\n<meta property=\"article:published_time\" content=\"2020-12-07T05:08:04+00:00\" \/>\n<meta property=\"article:modified_time\" content=\"2021-05-19T06:59:57+00:00\" \/>\n<meta name=\"author\" content=\"Santosh Kumar Shah\" \/>\n<meta name=\"twitter:card\" content=\"summary_large_image\" \/>\n<meta name=\"twitter:label1\" content=\"Written by\" \/>\n\t<meta name=\"twitter:data1\" content=\"Santosh Kumar Shah\" \/>\n\t<meta name=\"twitter:label2\" content=\"Est. reading time\" \/>\n\t<meta name=\"twitter:data2\" content=\"1 minute\" \/>\n<script type=\"application\/ld+json\" class=\"yoast-schema-graph\">{\"@context\":\"https:\/\/schema.org\",\"@graph\":[{\"@type\":\"WebPage\",\"@id\":\"https:\/\/santosh-shah.com\/blog\/how-to-modify-the-response-to-an-http-request-and-access-it-before-return-it-out-from-observable\/\",\"url\":\"https:\/\/santosh-shah.com\/blog\/how-to-modify-the-response-to-an-http-request-and-access-it-before-return-it-out-from-observable\/\",\"name\":\"How to modify the response to an HTTP request and access it before return it out from Observable? - Santosh Kumar Shah\",\"isPartOf\":{\"@id\":\"https:\/\/santosh-shah.com\/blog\/#website\"},\"datePublished\":\"2020-12-07T05:08:04+00:00\",\"dateModified\":\"2021-05-19T06:59:57+00:00\",\"author\":{\"@id\":\"https:\/\/santosh-shah.com\/blog\/#\/schema\/person\/b17cb45bdd5f518e74a08ad2c6c4b39f\"},\"breadcrumb\":{\"@id\":\"https:\/\/santosh-shah.com\/blog\/how-to-modify-the-response-to-an-http-request-and-access-it-before-return-it-out-from-observable\/#breadcrumb\"},\"inLanguage\":\"en-US\",\"potentialAction\":[{\"@type\":\"ReadAction\",\"target\":[\"https:\/\/santosh-shah.com\/blog\/how-to-modify-the-response-to-an-http-request-and-access-it-before-return-it-out-from-observable\/\"]}]},{\"@type\":\"BreadcrumbList\",\"@id\":\"https:\/\/santosh-shah.com\/blog\/how-to-modify-the-response-to-an-http-request-and-access-it-before-return-it-out-from-observable\/#breadcrumb\",\"itemListElement\":[{\"@type\":\"ListItem\",\"position\":1,\"name\":\"Home\",\"item\":\"https:\/\/santosh-shah.com\/blog\/\"},{\"@type\":\"ListItem\",\"position\":2,\"name\":\"How to modify the response to an HTTP request and access it before return it out from Observable?\"}]},{\"@type\":\"WebSite\",\"@id\":\"https:\/\/santosh-shah.com\/blog\/#website\",\"url\":\"https:\/\/santosh-shah.com\/blog\/\",\"name\":\"Santosh Kumar Shah\",\"description\":\"JavaScript Developer\",\"potentialAction\":[{\"@type\":\"SearchAction\",\"target\":{\"@type\":\"EntryPoint\",\"urlTemplate\":\"https:\/\/santosh-shah.com\/blog\/?s={search_term_string}\"},\"query-input\":{\"@type\":\"PropertyValueSpecification\",\"valueRequired\":true,\"valueName\":\"search_term_string\"}}],\"inLanguage\":\"en-US\"},{\"@type\":\"Person\",\"@id\":\"https:\/\/santosh-shah.com\/blog\/#\/schema\/person\/b17cb45bdd5f518e74a08ad2c6c4b39f\",\"name\":\"Santosh Kumar Shah\",\"image\":{\"@type\":\"ImageObject\",\"inLanguage\":\"en-US\",\"@id\":\"https:\/\/santosh-shah.com\/blog\/#\/schema\/person\/image\/\",\"url\":\"https:\/\/secure.gravatar.com\/avatar\/cf46c57219d897547f3204b6b302169b3302b17507ccc902946b622a78d0b98b?s=96&d=mm&r=g\",\"contentUrl\":\"https:\/\/secure.gravatar.com\/avatar\/cf46c57219d897547f3204b6b302169b3302b17507ccc902946b622a78d0b98b?s=96&d=mm&r=g\",\"caption\":\"Santosh Kumar Shah\"},\"description\":\"I am JavaScript developer.\",\"sameAs\":[\"https:\/\/santosh-shah.com\/blog\"],\"url\":\"https:\/\/santosh-shah.com\/blog\/author\/sks7yu\/\"}]}<\/script>\n<!-- \/ Yoast SEO plugin. -->","yoast_head_json":{"title":"How to modify the response to an HTTP request and access it before return it out from Observable? - Santosh Kumar Shah","robots":{"index":"index","follow":"follow","max-snippet":"max-snippet:-1","max-image-preview":"max-image-preview:large","max-video-preview":"max-video-preview:-1"},"canonical":"https:\/\/santosh-shah.com\/blog\/how-to-modify-the-response-to-an-http-request-and-access-it-before-return-it-out-from-observable\/","og_locale":"en_US","og_type":"article","og_title":"How to modify the response to an HTTP request and access it before return it out from Observable? - Santosh Kumar Shah","og_description":"We often need to modify the response that is return from the http request before it return as observable. That means before we parse it into our component we might need to update. It all depends on the version of RxJs. Angular 6 launches with RxJs 6 &#8211; which means that the map()\/catch() approach is [&hellip;]","og_url":"https:\/\/santosh-shah.com\/blog\/how-to-modify-the-response-to-an-http-request-and-access-it-before-return-it-out-from-observable\/","og_site_name":"Santosh Kumar Shah","article_published_time":"2020-12-07T05:08:04+00:00","article_modified_time":"2021-05-19T06:59:57+00:00","author":"Santosh Kumar Shah","twitter_card":"summary_large_image","twitter_misc":{"Written by":"Santosh Kumar Shah","Est. reading time":"1 minute"},"schema":{"@context":"https:\/\/schema.org","@graph":[{"@type":"WebPage","@id":"https:\/\/santosh-shah.com\/blog\/how-to-modify-the-response-to-an-http-request-and-access-it-before-return-it-out-from-observable\/","url":"https:\/\/santosh-shah.com\/blog\/how-to-modify-the-response-to-an-http-request-and-access-it-before-return-it-out-from-observable\/","name":"How to modify the response to an HTTP request and access it before return it out from Observable? - Santosh Kumar Shah","isPartOf":{"@id":"https:\/\/santosh-shah.com\/blog\/#website"},"datePublished":"2020-12-07T05:08:04+00:00","dateModified":"2021-05-19T06:59:57+00:00","author":{"@id":"https:\/\/santosh-shah.com\/blog\/#\/schema\/person\/b17cb45bdd5f518e74a08ad2c6c4b39f"},"breadcrumb":{"@id":"https:\/\/santosh-shah.com\/blog\/how-to-modify-the-response-to-an-http-request-and-access-it-before-return-it-out-from-observable\/#breadcrumb"},"inLanguage":"en-US","potentialAction":[{"@type":"ReadAction","target":["https:\/\/santosh-shah.com\/blog\/how-to-modify-the-response-to-an-http-request-and-access-it-before-return-it-out-from-observable\/"]}]},{"@type":"BreadcrumbList","@id":"https:\/\/santosh-shah.com\/blog\/how-to-modify-the-response-to-an-http-request-and-access-it-before-return-it-out-from-observable\/#breadcrumb","itemListElement":[{"@type":"ListItem","position":1,"name":"Home","item":"https:\/\/santosh-shah.com\/blog\/"},{"@type":"ListItem","position":2,"name":"How to modify the response to an HTTP request and access it before return it out from Observable?"}]},{"@type":"WebSite","@id":"https:\/\/santosh-shah.com\/blog\/#website","url":"https:\/\/santosh-shah.com\/blog\/","name":"Santosh Kumar Shah","description":"JavaScript Developer","potentialAction":[{"@type":"SearchAction","target":{"@type":"EntryPoint","urlTemplate":"https:\/\/santosh-shah.com\/blog\/?s={search_term_string}"},"query-input":{"@type":"PropertyValueSpecification","valueRequired":true,"valueName":"search_term_string"}}],"inLanguage":"en-US"},{"@type":"Person","@id":"https:\/\/santosh-shah.com\/blog\/#\/schema\/person\/b17cb45bdd5f518e74a08ad2c6c4b39f","name":"Santosh Kumar Shah","image":{"@type":"ImageObject","inLanguage":"en-US","@id":"https:\/\/santosh-shah.com\/blog\/#\/schema\/person\/image\/","url":"https:\/\/secure.gravatar.com\/avatar\/cf46c57219d897547f3204b6b302169b3302b17507ccc902946b622a78d0b98b?s=96&d=mm&r=g","contentUrl":"https:\/\/secure.gravatar.com\/avatar\/cf46c57219d897547f3204b6b302169b3302b17507ccc902946b622a78d0b98b?s=96&d=mm&r=g","caption":"Santosh Kumar Shah"},"description":"I am JavaScript developer.","sameAs":["https:\/\/santosh-shah.com\/blog"],"url":"https:\/\/santosh-shah.com\/blog\/author\/sks7yu\/"}]}},"_links":{"self":[{"href":"https:\/\/santosh-shah.com\/blog\/wp-json\/wp\/v2\/posts\/169","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/santosh-shah.com\/blog\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/santosh-shah.com\/blog\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/santosh-shah.com\/blog\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/santosh-shah.com\/blog\/wp-json\/wp\/v2\/comments?post=169"}],"version-history":[{"count":5,"href":"https:\/\/santosh-shah.com\/blog\/wp-json\/wp\/v2\/posts\/169\/revisions"}],"predecessor-version":[{"id":271,"href":"https:\/\/santosh-shah.com\/blog\/wp-json\/wp\/v2\/posts\/169\/revisions\/271"}],"wp:attachment":[{"href":"https:\/\/santosh-shah.com\/blog\/wp-json\/wp\/v2\/media?parent=169"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/santosh-shah.com\/blog\/wp-json\/wp\/v2\/categories?post=169"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/santosh-shah.com\/blog\/wp-json\/wp\/v2\/tags?post=169"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}