ARTICLE AD BOX
Our ASP.NET Core Web API needs to request data from an external web service (and no, the external web service cannot be called directly from the client) and return it without modification to the client.
Currently, the API handler reads all the data (XML) from the external request into memory and then returns the string as data for the EndPoint (see example).
Note: the code is simplified, so many checks regarding request success and authentication are left out
public async Task<ActionResult<string>> GetRemoteData(CancellationToken cancellationToken) { var xmlResponse = await response.Content.ReadAsStringAsync(cancellationToken); return Ok(xmlResponse); }But sometimes the response can become quite large (several MBs), so we'd rather not load the entire response into memory, but let it 'stream' from the external request as a response for our API call.
Does anyone have any pointers on where I should begin?
I've read these posts, but don't see any way to implement those without creating a file or allocating memory to hold all data (which I'm trying not to do):
Can I directly stream from HttpResponseMessage to file without going through memory? How to save Stream data from external GET request in .NET