question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

Wrong Response.text() behavior

See original GitHub issue

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

Issue Analytics

  • State:closed
  • Created 7 years ago
  • Comments:20 (5 by maintainers)

github_iconTop GitHub Comments

3reactions
vakrilovcommented, Jul 21, 2016

@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));
    });
1reaction
chiefmccommented, Jul 18, 2016

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

Read more comments on GitHub >

github_iconTop Results From Across the Web

Error Messages: Examples, Best Practices & Common Mistakes
Error message best practices. Good error messages and bad ones result in wildly different user behavior. Read this anecdote from UX pro Jennifer ......
Read more >
Handling Failed HTTP Responses With fetch() - TJ VanToll
Per MDN, the fetch() API only rejects a promise when a “network error is encountered, although this usually means permissions issues or similar....
Read more >
JavaScript fetch - Failed to execute 'json' on 'Response'
Response methode like 'json', 'text' can be called once, and then it locks. The posted image of response shows that body is locked....
Read more >
511289 - Non-ASCII characters not encoded correctly in Fetch ...
Issue 511289: Non-ASCII characters not encoded correctly in Fetch API's Response.text(). Reported by schedule a_deleted_user a_deleted_user.
Read more >
What happens when you read a response? - JakeArchibald.com
There's a bit of disagreement over the behaviour of requests and responses in the fetch API, curious to know what you think…
Read more >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found