title: Angular 6: How to set response type as text while making http call
body: <p>I trying to make http request to the spring rest API.. API returns a string value (&quot;success&quot; or &quot;fail&quot;)... but I dont know how to set the response type as string value while making call to the API..<strong>its throwing error as Backend returned code 200, body was: [object Object]</strong></p>
  <p>My angular code is like below,</p>
  <p>order.service.ts</p>
  <pre><code>import { Injectable } from '@angular/core';
  import { HttpClient, HttpHeaders } from '@angular/common/http';
  import { ProductSearch } from '../_models/product-search';
  import { ProductView } from '../_models/product-view';
  import { Observable } from 'rxjs';
  import { catchError } from 'rxjs/operators';
  import { ErrorHandlerService } from './error-handler.service';
  import { Category } from '../_models/category';
  
  
  @Injectable({
    providedIn: 'root'
  })
  export class OrderService {
  
    constructor(private http: HttpClient, private errorHandlerService: ErrorHandlerService) { }
  
  addToCart(productId: number, quantity: number): Observable&lt;any&gt; {
      const headers = new HttpHeaders().set('Content-Type', 'text/plain; charset=utf-8');
      console.log(&quot;--------order.service.ts----------addToCart()-------productId:&quot;+productId+&quot;:------quantity:&quot;+quantity);
       return this.http.post&lt;any&gt;('http://localhost:8080/order/addtocart', 
                { dealerId: 13, createdBy: &quot;-1&quot;, productId: productId, quantity: quantity}, 
                {headers: headers})
                .pipe(catchError(this.errorHandlerService.handleError));
      }
  }
  </code></pre>
  <p>error-handler.service.ts</p>
  <pre><code>import { Injectable } from '@angular/core';
  import { HttpErrorResponse, HttpResponse } from '@angular/common/http';
  
  import { Observable, throwError } from 'rxjs';
  import { catchError, retry } from 'rxjs/operators';
  
  @Injectable({
    providedIn: 'root'
  })
  export class ErrorHandlerService {
  
    constructor() { }
  
    public handleError(error: HttpErrorResponse) {
      if (error.error instanceof ErrorEvent) {
        // A client-side or network error occurred. Handle it accordingly.
        console.error('An error occurred:', error.error.message);
      } else {
        // The backend returned an unsuccessful response code.
        // The response body may contain clues as to what went wrong,
        console.error(
          `Backend returned code ${error.status}, ` +
          `body was: ${error.error}`);
      }
      // return an observable with a user-facing error message
      return throwError(
        'Something bad happened; please try again later.');
    };
  
  }
  </code></pre>