I have a url that return the html content with charset=iso-8859-7 which means angulars http request convert the data to utf8 by default and i am unable to encode them back in iso-8859-7 properly. After a lot of searching i found out that many people had the same issue and most of the answers were to change the charset in the server, something that i'm unable to do as the server doesn't belong to me.
So the question is how can HTTP request return binary so i can encode them to iso-8859-7 string?
EDIT - SOLUTION:
What i finally did was to to use TextDecoder and {responseType: ResponseContentType.ArrayBuffer} at RequestOptions. Here is an example to help anyone who is trying to parse html pages and decode them into the right encoding. Hope to help all the guys who tried this in a project which is using Angular2 like Ionic2.
public parser() {
// Set content type
let headers = new Headers({'Content-Type': 'application/x-www-form-urlencoded;'});
// Create a request option, with withCredentials for sending previous stored cookies
let options = new RequestOptions({
withCredentials: true,
headers: headers,
responseType: ResponseContentType.ArrayBuffer
});
return this.http.get('https://blablablablabla', options) // ...using get request
.map((res: any) => {
var string = new TextDecoder('iso-8859-7').decode(res._body);
var parser = new DOMParser();
var htmlDoc = parser.parseFromString(string, "text/html");
console.log(string)
..... blabla blabla doing some stuff here .....
})
.catch((error: any) => Observable.throw(error || 'Server error'));
}
Include the
RequestOptionsArgs
when you send request.Specify the field
responseType : ResponseContentType
insideRequestOptionsArgs
. (EitherResponseContentType.ArrayBuffer
orResponseContentType.Blob
)Use
TextDecoder
or something similar to decode the result.See the docs:
https://angular.io/api/http/Http
https://angular.io/api/http/RequestOptions
https://angular.io/api/http/ResponseContentType