3

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;
}
4

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.

  • Yes you are correct too. It sounds more like I need to redesign. – meditari Mar 2 '17 at 17:51
1

Some remarks in addition:

  • "501 Not Implemented" should be returned when a certain request (method) is not implemented at all (like for example there is simply no handler for a PUT request)

  • "405 Method not allowed" should be returned when the method handler does exist, but the API consumer has no rights to issue for example a PUT or DELETE request for that specific resource.

  • "400 bad request" should be returned if the request is syntactically incorrect, for example contains malformed JSON or XML

  • For validation errors, many APIs seem to use either "409 Conflict" or "422 Unprocessable Entity", though the latter is actually no part of the HTTP spec

This is at least how we handle these situations.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.