Michael
Michael

Reputation: 13616

What http status code to return if passed params not valid?

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.

Upvotes: 0

Views: 2074

Answers (1)

Nazmul Hasan
Nazmul Hasan

Reputation: 10590

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

Upvotes: 4

Related Questions