1

I work on web api project.

Here is action method:

public async Task<IHttpActionResult> GetDamageEvents(int siteObjectId, int statusId)
{
    try 
    {
        if (siteObjectId == null || siteObjectId == 0) return (what status?)
        if (statusId == null || statusId == 0) return (what status?)

        //some logic


        return Ok(some result);
    }
    catch (Exception)
    {
        return BadRequest();
    }
}

As you can see in method above I have two parameters siteObjectId and statusId,

if those parameters are null or zero I need to return to client appropriate status code.

What status code should it be?I think BadRequest status code it too general.

1
  • BadRequest is perfect. You can include message details to help your client out such as "Status must not be null or zero" so the client can fix the issue. Commented May 6, 2016 at 14:59

1 Answer 1

4

few developer thinking would have used Status 400 Bad Request:

The request could not be understood by the server due to malformed syntax. The client SHOULD NOT repeat the request without modifications.

on the other hand few developer Status 422 seems most appropriate

The 422 (Unprocessable Entity) status code means the server understands the content type of the request entity (hence a 415(Unsupported Media Type) status code is inappropriate), and the syntax of the request entity is correct (thus a 400 (Bad Request) status code is inappropriate) but was unable to process the contained instructions. For example, this error condition may occur if an XML request body contains well-formed (i.e., syntactically correct), but semantically erroneous, XML instructions.

which one you will be use, up to you

more check this link please http://www.bennadel.com/blog/2434-http-status-codes-for-invalid-data-400-vs-422.htm

Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.