Get the exact string return by the Ok() in Asp.NET Core 6

Viewed 32

I have RESTful API that returns some large JSON files. I want to save the content to a file before sending it to the client. Here's my endpoint now:

[HttpPost]
[Route("rest/result")]
public IActionResult GetResult(string requestId)
{
    var item = _service.GetItem(requestId);
    return item?.Result == null ? NotFound() : Ok(item.Result);
}

I tried this code:

[HttpPost]
[Route("rest/result")]
public async Task<IActionResult> GetResult(string requestId)
{
    var item = _service.GetItem(requestId);
    var str = JsonSerializer.Serialize(item.Result);
    await File.WriteAllTextAsync($"c:\\output\\{requestId}.json", str);
    return item?.Result == null ? NotFound() : Ok(item.Result);
}

But the file is a bit different. Probably in terms of encoding because I'm seeing things such as : "Data":"{\u0027FnId\u0027:\u002710000149\u0027 in the file.

1 Answers

That shouldn't be any problem, as both representations are equivalent.

\u0027 is Unicode for apostrophe ('). So, special characters are returned in Unicode but will show up properly when rendered on the page:

"Data":"{'FnId':'10000149'}"
Related