0
votes

I have this get request in my controller in ASP.NET Core project

    [HttpGet]
    [Route("api/controller/getlastresult/{id}")]
    public IActionResult GetLatestResultForController(string id)
    {
        Response.ContentType = "application/json";

        var list = _dbAccessLayer.GetAllVoteResults();
        var appropriateResults = list.Where(item => item.DeviceId.Equals(id) && item.Status == "Finished")
            .OrderBy(item => item.TimeStarted);

        if (appropriateResults.Any())
            return Ok(Json(appropriateResults.Last().Result));

        return BadRequest();
    }

It works fine, but in the headers there is Content-Type: application/json; charset=utf-8 as you can see here:

enter image description here

I need just regular application/json, without charset utf-8, so I guess it will be ASCII charset. Reason being that I use this request with microcontroller which does not like utf-8 encoding.

How do I set chaset in the response of my Get request? Thanks

1

1 Answers

0
votes

For removing charset, you could try middleware like

app.Use(async (context, next) =>
{
    context.Response.OnStarting((state) =>
    {
        if (context.Response.Headers.Count > 0 && context.Response.Headers.ContainsKey("Content-Type"))
        {
            var contentType = context.Response.Headers["Content-Type"].ToString();
            if (contentType.StartsWith("application/json"))
            {
                context.Response.Headers.Remove("Content-Type");
                context.Response.Headers.Append("Content-Type", "application/json");
            }
        }

        return Task.FromResult(0);
    }, null);
    await next.Invoke();
});