nativescript-angular: Wrong Response.text() behavior

Trying to get a simple string from a REST-API using the Angular’s Http.get(), but having an unexpected trouble while trying to get the Response’s body, using Response.text(): Expected response:

16288083cbfe40da68c4674b704c675f

Actual response:

<66363332 37303033 62306262 38646366 61356236 62353230 37616535 35613762>

I assume the problem is that the response from the server is not a JSON, nor HTML, but a plain text string and looks like it is stored internally using NSData, and there might be something wrong during string extraction from NSData.

versions: Angular 2.0.0-rc.4 TNS-core: 2.1.1 NativeScript-Angular 0.2.1

About this issue

  • Original URL
  • State: closed
  • Created 8 years ago
  • Comments: 20 (5 by maintainers)

Most upvoted comments

@chiefmc After some investigation: What you are seeing is actually toString-ed NSData of the response. There is a pending fix: NativeScript/NativeScript#2487

There is a workaround(not very pretty though). Get the response body and call IOS native API to convert it to string:

import {Http, Headers, Response} from "@angular/http";
import * as platform from "platform";

// ...

declare var NSString: any;
function getResponseText(response: Response): string {
    if (platform.isIOS) {
        const nsData = (<any>response)._body;
        return NSString.alloc().initWithDataEncoding(nsData, 4).toString();
    }
    return response.text();
}

// ...

this._http.get("https://breakouttrampoliningwebservices.azurewebsites.net/test/guid")
    .subscribe(response => {
        console.log("TEXT: " + getResponseText(response));
    });

I must mention, that the same code works perfectly in Android environment. So the problem most probably lies somewhere in-between NS and iOS