Ritu

Ritu

  • 1.4k
  • 347
  • 42.8k

consume web api without database

May 19 2021 2:37 PM
initally i have json file in which some get methods are defined and i don't have database and i want consume web api in asp.net core console application and read json file from json file and send data to database.
 
url which i have passes in main method (which is access through postman, impport the json file in postman and retrieved some GET methods).
 
so please help me in this code
 
how to consume web service and send data to database.
 
i have created ue_GDL_CustAddrs.cs which contains
  1. public class ue_GDL_CustAddrs  
  2. {  
  3. public string Name { getset; }  
  4. public string Addr1 { getset; }  
  5. public string Addr2 { getset; }  
  6. }  
  7. public async void GET(string url)  
  8. {  
  9. //string result = string.Empty;  
  10. HttpWebRequest httpWebRequest = (HttpWebRequest)WebRequest.Create(url);  
  11. httpWebRequest.Method = "Get";  
  12. StreamReader reader;  
  13. try  
  14. {  
  15. HttpClient client = new HttpClient();  
  16. client.BaseAddress = new Uri(url);  
  17. client.DefaultRequestHeaders.Accept.Add(new MediaTypeWithQualityHeaderValue("application/json"));  
  18. HttpResponseMessage response = client.GetAsync("https://csi10a.erpsl.inforcloudsuite.com/IDORequestService/ido/load/ue_GDL_CustAddrs").Result;  
  19. if (response.IsSuccessStatusCode)  
  20. {  
  21. IEnumerable < ue_GDL_CustAddrs > Addr = response.Content.ReadAsAsync<IEnumerable<ue_GDL_CustAddrs>>().Result;  
  22. foreach (var p in Addr)  
  23. {  
  24. Console.WriteLine("{0}\t{1};\t{2}", p.Name, p.Addr1, p.Addr2);  
  25. Console.ReadLine();  
  26. }  
  27. }  
  28. else  
  29. {  
  30. Console.WriteLine("{0} ({1})", (int)response.StatusCode, response.ReasonPhrase);  
  31. }  
  32. }  
  33. catch (WebException ex)  
  34. {  
  35. WebResponse errorResponse = ex.Response;  
  36. using (Stream responseStream = errorResponse.GetResponseStream())  
  37. {  
  38. reader = new StreamReader(responseStream, System.Text.Encoding.GetEncoding("utf-8"));  
  39. String errorText = reader.ReadToEnd();  
  40. }  
  41. throw;  
  42. }  
  43. }  
i have send the url through main method but got an exception
 
One or more errors occurred. (Cannot deserialize the current JSON object (e.g. {"name":"value"}) into type 'System.Collections.Generic.IEnumerable`1[Englert_Ewarranty_QAD_Integration_Details.ue_GDL_CustAddrs]' because the type requires a JSON array (e.g. [1,2,3]) to deserialize correctly.
 
To fix this error either change the JSON to a JSON array (e.g. [1,2,3]) or change the deserialized type so that it is a normal .NET type (e.g. not a primitive type like integer, not a collection type like an array or List<T>) that can be deserialized from a JSON object. JsonObjectAttribute can also be added to the type to force it to deserialize from a JSON object.
Path 'Items', line 2, position 10.)'

Answers (2)