REST API Framework. Recommended behavior for invalid querystring parameter

前端 未结 2 1652
清酒与你
清酒与你 2020-12-29 07:45

I am implementing a REST API Framework, and I wonder what the recommendedbehavior is, when a client submits an invalid querystring parameter.

I will illustrate what

2条回答
  •  忘掉有多难
    2020-12-29 08:09

    Since this is a REST call, we are talking about resources. And whenever we have a wrong filter, we should return a proper error code.

    In this case i would go for 400 - bad request as the resource was found and correctly mapped (/api/contacts), but there was a problem with the query string part. Therefore a 400 and not a 404.

    Would return a 404 if someone requested /api/contacts-all or some non-existant resource.

    EDIT based on comments below

    Agree to your comment. Ideally a 400 is a problem with the request. Going by that, you could use a 422 Unprocessable Entity. Please look at the stackoverflow link below and it talks about the same thing.

    I would guess that developers around the world would be more comfortable seeing a 400 than 422 for such logical errors due to the fact that bigger companies are using 400 and not 422.

    References: Http status codes and 400 for logical error vs malformed request

提交回复
热议问题