prasanna p

prasanna p

  • 1.2k
  • 465
  • 96.6k

how to read Web APi csv file

Nov 18 2020 4:39 AM
Hi Friends,
 
To read data from our client API, first I am login into the URL https://site.com by using username, password and location.
 
After login to get the data another Web API URL is there https://site.com/users/reports/csv?location_id=42&from_date=2020-11-02&to_date=2020-11-05
 
After login, we used this URL the file is automatically downloading in the browser.
 
I want to read this CSV file content and I need to insert the records into the database.
 
for web Api login I wrote this code
  1. var userName = "[email protected]";  
  2. var passwd = "5CadcPT";  
  3. var url = "https://site.com";  
  4. var location= "Hyderabad";  
  5. var client = new HttpClient();  
  6. var authToken = Encoding.ASCII.GetBytes($"{userName}:{passwd}:{location}");  
  7. client.DefaultRequestHeaders.Authorization = new AuthenticationHeaderValue("Basic",  
  8. Convert.ToBase64String(authToken));  
  9. var result1 = await client.GetAsync(url);  
  10. var content = await result1.Content.ReadAsStringAsync();  
  11. For downloading file I used this code file is downloading but the HTML content is coming in the downloaded CSV file.  
  12. stream = await response.Content.ReadAsStreamAsync();  
  13. var fileInfo = new FileInfo("reports_2020-11-10 08_14_12.csv");  
  14. using (var fileStream = fileInfo.OpenWrite())  
  15. {  
  16. await stream.CopyToAsync(fileStream);  
  17. }  
how to do this?

Answers (4)