2

In one of the Net API controllers, I have an action which sends the request to another third party API and after receiving a response it does some manipulation with the JSON data received and finally returns the response to the frontend application. Sample code is given below:

using (var client = new HttpClient())
{
    var response = await client.GetAsync(_remoteServiceEndPoint + "student/" + id);
    if (response.IsSuccessStatusCode)
    {
        var contentString = await response.Content.ReadAsStringAsync();
        //do something with contents and finally return response to the caller
    }
}

Now if the third party sends a JSON response which contains a number like 999999999999999.99 and the ReadAsStringAsync executes in the above code snippet, it changes the value to 10000000000000000. Which I don't want.

I would like to know why ReadAsStringAsync is behaving like this.

A J Qarshi
  • 2,772
  • 6
  • 37
  • 53
  • 2
    Are you sure the call to `ReadAsStringAsync()` is causing the problem? I'd suspect that to work fine, but you'd get a problem when deserializing the string with NewtonSoft. – Jesse de Wit Apr 26 '19 at 12:04
  • This sounds like a bug that should be reported in the corefx repo, but I agree with Jesse - I can't see any reason why `ReadAsStringAsync` would ever need or want to modify the data it's retrieving. – Ian Kemp Apr 26 '19 at 12:13
  • @JessedeWit My bad. It's `JObject.Parse(contentString)` causing the issue. – A J Qarshi Apr 26 '19 at 12:46
  • 1
    @AJQarshi Great, I was sweating a bit for all the calls to `ReadAsStringAsync` I'd written in the past. – Jesse de Wit Apr 26 '19 at 12:48

1 Answers1

1

Thanks to @JessedeWit for pointing out that it's an issue with NewtonSoft library. Introducing the workaround mentioned in this SO question response solved the issue.

A J Qarshi
  • 2,772
  • 6
  • 37
  • 53