ASP.NET Core Web API Interview Question #15

When should a Web API return 200 OK?

Web API & REST Fundamentals, Controllers, HTTP Methods & Status Codes Junior Beginner

Quick Interview Answer

200 OK indicates that the request succeeded and the response contains a representation or result. It is commonly used for successful GET requests and for other successful operations that intentionally return a response body.

Detailed Explanation

200 OK indicates that the request succeeded and the response contains a representation or result. It is commonly used for successful GET requests and for other successful operations that intentionally return a response body.

If a successful operation has no response content, 204 No Content may communicate the result more precisely.

Code Example

[HttpGet("{id:int}")]
public async Task<IActionResult> Get(int id)
{
    var product = await _service.GetAsync(id);

    if (product == null)
        return NotFound();

    return Ok(product);
}