0

I am having problems with a json response containing special characters. In my code I have request like this:

const headers = new HttpHeaders({'Content-Type':'application/json; charset=utf-8'});
this.HttpClent.get<any>(my_url, {headers})
    .subsrcibe(res => console.log(res));

If I look at the preview in my navigator (Firefox or Chrome give the same result) I have this:

enter image description here

but the console.log give me this:

enter image description here

As you can see all the special characters, that were correctly displayed in the preview, are now broken in the object obtained from the response.

Any idea?

Alessandro_V
  • 25
  • 1
  • 5
  • 1
    Is this only a problem in `console.log`, or is it a genuine problem elsewhere? – Kurt Hamilton Mar 24 '20 at 09:53
  • One possible cause is that the original file (data) is saved in the wrong encoding and not UTF-8, so the it has to be re-encoded. – Mikko Ohtamaa Mar 24 '20 at 09:57
  • @KurtHamilton the console.log is just to visualize it. If you look 'inside' the object in the debugger the strings are broken. – Alessandro_V Mar 24 '20 at 10:08
  • @MikkoOhtamaa. My understanding was that, if the encoding was wrong, the preview as well should be broken – Alessandro_V Mar 24 '20 at 10:08
  • 1
    @Alessandro_V: Not essentially, the string shown in preview was decoded by the browser whereas the decoding is not handled by HttpClient in Angular explicitly. See [here](https://github.com/dart-lang/http/issues/175#issuecomment-405103945) for more info. Please check if [this](https://stackoverflow.com/a/13691499/6513921) answer solves the issue. – ruth Mar 24 '20 at 13:03
  • @MichaelD: thanks for the input. Unfortunately that doesn't seem to solve the issue. `escape(decodeURIComponent(res[1].layer))` gives `"%uFFFDta"` that is actually the escaped replacement character – Alessandro_V Mar 24 '20 at 14:18
  • I believe it should be `decodeURIComponent(escape(res[1].layer))`. – ruth Mar 24 '20 at 14:20
  • Tested as well ;) It gives `"�ta"` – Alessandro_V Mar 24 '20 at 14:22

0 Answers0