Angular HttpClient Can Corrupt Large Numbers

Thanoshan MV
2 min readOct 16, 2024

--

Problem

Let’s say, the API returns 638564648276578546 (a timestamp in C# ticks) to the Angular application. The Angular HttpClient parses the value into JSON and returns as 638564648276578600 to the caller. The JSON parse corrupts the large numbers. Please refer to this article for the reason: Why does JSON.parse corrupt large numbers and how to solve this?

There are two possible solutions. There can be more. Please let me know if you know more than these solutions.

Solution 1

The API returns the value inside the model than just returning the value itself.

public class Timestamp
{
public string Value { get; set; }
}

Solution 2

Let the Angular HttpClient to parse the body as text. It will not corrupt the value. It’ll return 638564648276578546 to the caller.

For that, just add responseType: 'text'

return this.http.get(
'URL', {
responseType: 'text'
});

References for the further exploration:

--

--

Thanoshan MV
Thanoshan MV

Written by Thanoshan MV

Hi! I'm a Software Engineer with a passion for research in AI, software engineering, and open-source development. Web - https://thanoshanmv.github.io/