1

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.

4
  • Which editor are you using? VS Code? Commented Jul 3, 2019 at 7:41
  • Yes, I'm using visual code Commented Jul 3, 2019 at 7:43
  • 4
    Can you try to use responseType: 'text' as 'json' in your headers? Commented Jul 3, 2019 at 7:48
  • That's fixed the error, thanks Commented Jul 3, 2019 at 7:53

1 Answer 1

1

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
    );
  }
Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.