0

I have a REST service in Angular which returns JSON response. I need to get the response as string to do some pattern matching and value replacement.

I am using Angular 7. Below is my service.

getUIData(): Observable<any> {
    const url = `${this.baseUrl}/uiData`;
    return this.http.get<any>(url).pipe(
      catchError(this.handleError<any>('Get Data:'))
    );
  }
1
  • 1
    Use the map operator. Commented Jul 24, 2019 at 5:47

2 Answers 2

1

By default HttpClient will return JSON Object

in your case you need to convert it to String. So do some changes like below,

getUIData(): Observable<any> {
    const url = `${this.baseUrl}/uiData`;
    return this.http.get<any>(url).map( response => JSON.stringify(response.data)).
      catchError(this.handleError<any>('Get Data:');
  }

as @dcg suggested use map instead of pipe to convert your response data to string in the Service method itself.

I hope it helps :).

Sign up to request clarification or add additional context in comments.

Comments

1

You can do this in two ways :

1) set request headers as :

'Content-Type', 'text/plain; charset=utf-8'

2) Transform your response in stringified format inside map operator

JSON.stringify(data);

Note for second. it will still be JSON but stringified

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.