1

I make a Get call to my API and want the result as a string so I can split it

  GetServiceProviderId() {
   this.http.get(this.rooturl + 'info', { headers: this.reqHeader });

    var data = "mitId: 18, ServiceProvider: 2" <- result I get and want to split
    var dataspilitted = data.split(" ");
    return dataspilitted[3];
  }
Ole Bark
  • 109
  • 2
  • 13
  • Possible duplicate of [Angular HttpClient "Http failure during parsing"](https://stackoverflow.com/questions/46408537/angular-httpclient-http-failure-during-parsing) – Kirk Larkin Aug 17 '18 at 13:10

3 Answers3

3

Original guide says you need to specify responseType in request options

GetServiceProviderId() {
    return this.http.get(this.rooturl + 'info', { headers: this.reqHeader, responseType:'text' })
        .map(data => {
    var dataspilitted = data.split(" ");
    return dataspilitted[3];
    });
}
1

Isn't that a JSON response? Why convert to string, split, get the 3rd word, convert to number and hope that nothing changes?

you could just do:

this.http.get(this.rooturl + 'info', { headers: this.reqHeader })
  .subscribe(result => console.log('Result is ', result.ServiceProvider));
Andrei Tătar
  • 7,872
  • 19
  • 37
0

You will need to subscribe to the HTTP call and then parse the data from the response depending on how it is formatted.

GetServiceProviderId() {
    this.http.get(this.rooturl + 'info', { headers: this.reqHeader }).subscribe(
        (data) => {
            // Optionally parse "data" into string depending on result format
            // data = parse(data)
            this.dataSplit = data.split(" ")[3];
        }
    );
}

In this example parse(data) is some function that you would write to parse the data into a string, if the HTTP response does not return a string in the first place.

Tim Klein
  • 2,538
  • 15
  • 19