C
C#13mo ago
PIE

Attempting to get the file name from a REST api download link

I have a method that that recive a zip file from a REST api download link. However, the method that I use only manages to get the id of the file that I originally sent out. This was my method to extract the filename from the api:
public static string GetFilenameFromUrl(String url)
{
Uri uri = new Uri(url);
return System.IO.Path.GetFileName(uri.AbsolutePath);
}
public static string GetFilenameFromUrl(String url)
{
Uri uri = new Uri(url);
return System.IO.Path.GetFileName(uri.AbsolutePath);
}
and my method to download the zipped file form the api
public void DownloadBeatmapById(int id, string path)
{
string fileName = GetFilenameFromUrl($"https://api.chimu.moe/v1/download/{id}");

Task<Stream> stream = s_httpClient.GetStreamAsync($"https://api.chimu.moe/v1/download/{id}");

Console.WriteLine(path);
Console.WriteLine(fileName);
//.osz is another version of .zip
FileStream fileStream = new FileStream(string.Concat(path + fileName + ".osz"), FileMode.OpenOrCreate);

stream.Result.CopyTo(fileStream);
}
public void DownloadBeatmapById(int id, string path)
{
string fileName = GetFilenameFromUrl($"https://api.chimu.moe/v1/download/{id}");

Task<Stream> stream = s_httpClient.GetStreamAsync($"https://api.chimu.moe/v1/download/{id}");

Console.WriteLine(path);
Console.WriteLine(fileName);
//.osz is another version of .zip
FileStream fileStream = new FileStream(string.Concat(path + fileName + ".osz"), FileMode.OpenOrCreate);

stream.Result.CopyTo(fileStream);
}
So for example if I called the method with GetFilenameFromUrl(1627149); it would return the id (1627149) instead if the default filename (ex. the filename received when putting the link https://api.chimu.moe/v1/download/1627149 in the browser). Is there an alternative way to get the zipped file similar to a browser?
2 Replies
Buddy
Buddy13mo ago
The file name should be in the headers
PIE
PIEOP13mo ago
Ah yeah I figured it out it turns out you can't access the headers with httpClient.GetStreamAsync so I had to use httpClient.GetAsync to get an HttpResponseMessage which I can get the headers :)
Want results from more Discord servers?
Add your server