I am attempting to update the RestSharp file download portion code in one of my applications. Apparently the .SaveAs()
is being depricated, so I'm trying to follow their updated example for working with files. However, my response is always null, and the temp file that is created doesn't seem to be filled with the data I'm attempting to save.
Here's what I have so far:
var tempFile = Path.GetTempFileName();
using var writer = File.OpenWrite(tempFile);
var client = new RestClient("https://provider-api.spotify.com/v1/analytics");
var request = new RestRequest("{licensor}/enhanced/tracks/{year}/{month}/{day}", Method.GET);
request.AddHeader("Authorization", $@"Bearer {token}");
request.AddUrlSegment("licensor", "licensor_name");
request.AddUrlSegment("year", 2021);
request.AddUrlSegment("month", 1);
request.AddUrlSegment("day", 10);
var checkResponse = client.Execute<SpotifyTracksResourceModel>(request);
if (checkResponse.Content == "")
{
Console.WriteLine("No data");
}
request.ResponseWriter = responseStream =>
{
using (responseStream)
{
responseStream.CopyTo(writer);
}
};
var response = client.DownloadData(request);
I threw in the checkResponse
code to ensure that I am actually getting data back, and I am in fact getting data. But as I said, once it gets to the var response = ...
line, it comes back NULL, and nothing has been written to that temp file.
Thank you in advance for any help with this!
question from:
https://stackoverflow.com/questions/65831326/null-response-when-using-restsharp-to-download-a-file 与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…