Is there a better way to check if a JSON request accepts?

Actually, I use this way. Do you have a better way?

private bool AcceptJson(HttpRequest request)
{
    const string JsonType = "application/json";

    if (request.ContentType.ToLower(CultureInfo.InvariantCulture).StartsWith(JsonType))
    {
        return true;
    }

    if (request.AcceptTypes.Select(t => t.ToLower(CultureInfo.InvariantCulture) == JsonType).Count() > 0)
    {
        return true;
    }

    return false;
}
+3
source share
2 answers

This approach can lead to false positives (it does not take into account q values โ€‹โ€‹or content types, which application / json is a substring).

You can find a decent Accept header parser in this XHTML article . You will have to transfer the algorithm to your chosen language and adapt it for the types of content that you use.

+2
source

, "". , , . , Select Count.

+1

Source: https://habr.com/ru/post/1697936/


All Articles