Api call, client set up process part

I am trying to set up my client.cs file for a api call that in order to pull data via postman you need to provide username and password under the authorization portion as a BasicAuth type, as well as Key and Value (that is added to the Header) under the APIKey Type. However, the api call keep failing with status = NotFound any suggestions are appreciated

 public async Task<List<Assets>> GetAllAssets()
 {
     var assets = new List<Assets>();

     var credentials = new NetworkCredential($"{_Username}", $"{_Password}");

     using (var handler = new HttpClientHandler { Credentials = credentials })
     using (var client = new HttpClient(new HttpClientHandler()))
     {
         client.BaseAddress = new Uri(AppConfig.SpireonApiBaseUrl);
         client.DefaultRequestHeaders.Add("Accept", "application/json");
         client.DefaultRequestHeaders.Add("X-Nspire-UserToken", $"{_apiToken}");
         client.DefaultRequestHeaders.Add("Username", _Username);
         client.DefaultRequestHeaders.Add("Password", _Password);

         var hasNextPage = true;
         int currentPage = 1;

         do
         {
             var uri = new UriBuilder(new Uri($"{AppConfig.SpireonApiBaseUrl}/assets"));
           
             var request = new HttpRequestMessage(HttpMethod.Get, uri.Uri);

             var response = await client.SendAsync(request);

             if (response.StatusCode != HttpStatusCode.OK)
             {
                 throw new Exception($"Call to {request.RequestUri} failed with status {response.StatusCode}. " +
                                             //$"{Environment.NewLine}Request Body:{Environment.NewLine}{body}{Environment.NewLine}{Environment.NewLine}" +
                                             $"Response Body:{Environment.NewLine}{response.Content.ReadAsStringAsync().Result}{Environment.NewLine}");
             }

             var content = response.Content.ReadAsStringAsync();
             

         } while (hasNextPage);
     }

     return assets;
 }

  • Capture the request (using Fiddler or a similar tool) that is sent from your program and compare it to a known-good request (e.g. one that you sent from Postman). Check for any differences.

    – 

Leave a Comment