title: Setting Response Type in Http Request in Angular 5
body: <p>I am currently working on migrating our application from Angular 4 to Angular 5. I have a file which handles any post request and the post method looks like this:</p>
  
  <pre><code>public post = (data: any, url = '', header: any = null, mapToJson = false): any =&gt; {
          if (mapToJson) {
              return this.commonHttpService.post(url, data, header)
                  .map(this.extractData)
                  .map((response: Response) =&gt; response.json());
          } else {
              return this.commonHttpService.post(url, data, header)
                  .map(this.extractData)
                  .map((response: Response) =&gt; response);
          }
      }
  </code></pre>
  
  <p>It is making a call to the common http service method which is written in other file where I am creating a Http Request and appending the headers, params and options to the request. In the options we need to mention the response type, so I have added 'json' as response type. But some of the web services might return text or blob or any other content. The following is the way I have declared options in the request:</p>
  
  <pre><code> httpOptions = {
                  body: body,
                  headers: req.headers,
                  observe: 'response',
                  params: params,
                  reportProgress: false,
                  responseType: 'json',
                  withCredentials: true,
  
              };
      request = this.http.request('POST', url, httpOptions);
  </code></pre>
  
  <p>So when I am getting the response type as 'Text' or 'Blob' then the mapping is failing.It looks for a json structure but since I am getting the response as “Success”,I get an error about parsing. Is there any way we can specify multiple response types in the options. Like for 'responseType' inside httpOptions I would like to pass multiple values like 'json' | 'text' | 'blob'. Or Can we specify the response type dynamically? I have searched for answers online and was not able to find a better approach for this. </p>