1
votes

I'm getting the following error when trying to call a service using rest api and a get call:

Argument of type '{ headers: HttpHeaders; responseType: string; }' is not assignable to parameter of type '{ headers?: HttpHeaders | { [header: string]: string | string[]; }; observe?: "body"; params?: HttpParams | { [param: string]: string | string[]; }; reportProgress?: boolean; responseType?: "json"; withCredentials?: boolean; }'. Types of property 'responseType' are incompatible. Type 'string' is not assignable to type '"json"'.ts(2345)

 const httpOptionsText = {
  headers: new HttpHeaders({
    'Accept': 'text/plain',
    'Content-Type': 'text/plain'
  }),
  responseType: 'text'
};

Here is the service call with httpOptionsPlain as parameter which is error is signed.

@Injectable()
export class TripService {

public getTripNameById(tripId: number): Observable<any> {
    return this.http.get<any>(`${this.baseUrl}/Trips/Trip/Name/${tripId}`,  httpOptionsText);
  }

The error is on the editor only (the code works fine). Thanks.

1
Which editor are you using? VS Code?Eugene
Yes, I'm using visual codeuser2304483
Can you try to use responseType: 'text' as 'json' in your headers?Bartu
That's fixed the error, thanksuser2304483

1 Answers

0
votes

You should change your code to

  public getTripNameById(tripId: number): Observable<any> {
    const httpOptionsText = {
      headers: new HttpHeaders({
        Accept: "text/plain",
        "Content-Type": "text/plain"
      }),
      responseType: "text" as "json"
    };
    return this.http.get<any>(
      `${this.baseUrl}/Trips/Trip/Name/${tripId}`,
      httpOptionsText
    );
  }