1

I am trying to make an API call to a server where the response is in JSON format and it is extremely lengthy. I think that the limits of a string cannot hold the entire response, as I am only getting part of it. I'm not sure how to retrieve the whole response, as i need to read certain parts throughout the response (there is a repeating structure for each location where I need to find data, for example the name). Here is the code I have so far, with private info being excluded:

            using (var httpClient = new HttpClient())
        {
            httpClient.BaseAddress = new Uri("ADDRESS HERE");
            httpClient.DefaultRequestHeaders.Add("Api-Key", "API KEY HERE");
            httpClient.DefaultRequestHeaders.Accept.Add(
            new MediaTypeWithQualityHeaderValue("application/hal+json"));

            var response = httpClient.GetAsync("locations/").Result;
            string res = "";
            using (HttpContent content = response.Content)
            {
                Task<string> result = content.ReadAsStringAsync();
                res = result.Result;
            }

How could I obtain the entire response? Do I need to parse it? And if so please point me in the right direction to documentation or other good answered questions. I am new to C# and API calls, but C# seemed to handle it the best in my situation. Thank you.

Eman936
  • 79
  • 9
  • See the linked question, and obtain a stream using `content.ReadAsStreamAsync()` instead. If you don't feel the duplicate answers your question, let me know (tag me @john) and I'll reopen your question :) – ProgrammingLlama May 16 '19 at 01:34
  • 1
    Handling large (multi-MB) Http responses is actually quite difficult to do efficiently in any API, not only .NET. Read [You are Using HttpClient Wrong](https://josefottosson.se/you-are-probably-still-using-httpclient-wrong-and-it-is-destabilizing-your-software/). – Dour High Arch May 16 '19 at 01:37

0 Answers0