REST Web API – What to Return: NotImplemented, BadRequest, or Bad Design?

httprestweb-api

Let's say I have an enumeration of related information types and an enumeration of time periods. Also I have created two operations on a controller for retrieving data of those info types.

Due to third party API constraints InfoA can only be retrieved with a date range and InfoB and InfoC can only retrieved with a time period enumeration. In addition, for the sake of extensibility, I have made the selected InfoType as a parameter in both operation paths because I know further requirements are coming from the business at a later date and also because I don't want to create a new operation for every InfoType.

So when checking the InfoType in the operation and it is found to not be supported should I return a HttpStatusCode.NotImplemented or HttpStatusCode.BadRequest because of a bad parameter? Or is this a bad solution?

public enum InfoType
{
    InfoA = 0,
    InfoB = 1,
    InfoC = 2,
}

public enum TimePeriod
{
    Previous = 0,
    Current = 1,
}

[Route("api/v1/info/{selectedInfoType:int}/{startDate:datetime}/{endDate:datetime}")]
public HttpResponseMessage Get(InfoType selectedInfoType, DateTime startDate, DateTime endDate)
{
    HttpResponseMessage result;
    string msg = string.empty;

    switch(infoType)
    {
        case InfoType.InfoA:
            result = ...do some work
            break;
        default:
            msg = "Not implemented for this InfoType";
            break;
    }

    if(!string.IsNullOrEmpty(msg))
    {
        result = new HttpResponseMessage(HttpStatusCode.NotImplemented);
    }

    return result;
}

[Route("api/v1/info/{selectedInfoType:int}/{selectedTimePeriod:int}")]
public HttpResponseMessage Get(InfoType selectedInfoType, TimePeriod selectedTimePeriod)
{
    HttpResponseMessage result;
    string msg = string.empty;

    switch(infoType)
    {
        case InfoType.InfoB:
            result = ...do some work
            break;
        case InfoType.InfoB:
            result = ...do some work
            break;
        default:
            msg = "Not implemented for this InfoType";
            break;
    }

    if(!string.IsNullOrEmpty(msg))
    {
        result = new HttpResponseMessage(HttpStatusCode.NotImplemented);
    }

    return result;
}

Best Answer

The correct response code for any URL that doesn't exist is 404. That's the situation you're describing. The client is asking for a URL that the server has no resource for.

Related Topic