A GET endpoint ASP.NET Core Web API will return a 200 OK with a response body. But on every 3rd call to the API different status code should be sent

78 views Asked by At

When the GET endpoint is called, it returns a 200 status code with a response body. But on every third call to the API, a different status should be returned with empty response body. How to track the count on the API calls?

Response for first and second calls:

Status Code: 200

Response: {
    message: "OK"
}

Response for third call to the API:

Status Code: 503

Response: {
    
}
1

There are 1 answers

1
Yuning Duan On

Under normal circumstances, in order to meet security and business logic needs, we often set relevant access limit in the interface. However, access restrictions on interfaces usually not only limit the number of accesses, but you can also use a dictionary to store IP addresses and set the corresponding number of accesses to limit the number of accesses to user IP addresses and other identifiers, so that the system can be more comprehensive. and comprehensive. In your problem description, you can use a variable to save the number of calls. This count is incremented each time the API is called. When the count reaches 3 times, modify the returned status code and response body. The variable is incremented every time the API is called. Then, allow calling logic that returns a different status code depending on the value of the variable, and below is a use case that you can use as a reference:

public class CountController : ControllerBase
{

    private static int callCount = 0;

    [HttpGet]
    public IActionResult Get()
    {
        callCount++;

        var statusCode = (callCount % 3 == 0) ? 503 : 200;

        var message = (statusCode == 503) ? "Service not available" : $"OK, Call Count: {callCount}";
 
        if (callCount % 3 == 0)
        {
            return StatusCode(statusCode);
        }

        return StatusCode(statusCode, new { message });
    }
}

If you're using a distributed system, you could store cached data in memory that is shared across all instances of your application cluster. Then you can use AddDistributedMemoryCache so that different application instances can share the same cached data. It uses ASP.NET Core's distributed caching mechanism and is suitable for distributed applications:

 private readonly IDistributedCache _cache;

    public YourController(IDistributedCache cache)
    {
        _cache = cache;
    }

    [HttpGet]
    public IActionResult Get()
    {

        var callCountBytes = _cache.Get("callCount");

        var callCount = (callCountBytes != null) ? BitConverter.ToInt32(callCountBytes, 0) + 1 : 1;

        var statusCode = (callCount % 3 == 0) ? 503 : 200;

        var message = (statusCode == 503) ? "Service not available" : $"OK, Call Count: {callCount}";
    
        if (callCount % 3 == 0)
        {
            return StatusCode(statusCode);
        }
 
        _cache.Set("callCount", BitConverter.GetBytes(callCount));

        return StatusCode(statusCode, new { message });
    }
}

When I used the get method the first two times:

enter image description here enter image description here

When I used the get method for the third time:

enter image description here