frostshoxx
frostshoxx

Reputation: 536

ASP.NET Core: Is it possible to use HttpClient to fetch a file and return directly?

I have an internal API that would fetch and return a fileresult. However, this API does not have any concept of authentication/role/permission checks and cannot be modified to do so.

I would like to create an web API endpoint on an existing ASP.NET Core 2 Web API to do permission check, make a call to this internal API and return the fileresult back to a web client.

Would it be possible to get the wrapper API endpoint to just pass whatever it fetches as a file result without having to reconstruct the response (e.g., specify file name, content type, etc)? The files could be images, pdfs, document. I would prefer that this wrapper API only do permission check and make a call to the internal API endpoint using some sort of fileId and not need to know about the content length or type.

Upvotes: 2

Views: 5197

Answers (1)

frostshoxx
frostshoxx

Reputation: 536

Per @chris-pratt's recommendation, turned out it wasn't that complicate to reconstruct the result as I originally anticipated. I ended up implementing this way in case someone needs to do something similar here.

... some validation logic outside the scope of the question...
using (HttpClient client = new HttpClient())
                {
                    var file = await client.GetAsync($"{someURL}/{id}");
                    return new FileContentResult(await file.Content.ReadAsByteArrayAsync(), file.Content.Headers.ContentType.MediaType);
                }

Upvotes: 6

Related Questions