简体   繁体   中英

REST api for field validation

I have to create a couple of web services for validating possible values of given fields. I'm contemplating having something like:

POST /entity/fieldName body { fieldValues }

where the POST will return 400 (Bad request) if the arguments are invalid and 422 (Unprocessable entity) otherwise. However I do not really like the 422 response part very much since it makes the request always return an error. On the other hand since I'm only doing validation and this is a POST I don't want to actually create a new resource on the server (ie return 200). Is there another HTTP method / API endpoint that is better suit for this? For what it's worth I will be checking that the entity field with <fieldName> has its value in a given range.

If all you do is validating, then I think you should send 422 by validation error and 200 by validation success. The POST does not mean you have to always create a new entity.

The action performed by the POST method might not result in a resource that can be identified by a URI. In this case, either 200 (OK) or 204 (No Content) is the appropriate response status, depending on whether or not the response includes an entity that describes the result.

If a resource has been created on the origin server, the response SHOULD be 201 (Created) and contain an entity which describes the status of the request and refers to the new resource, and a Location header (see section 14.30).

I'm prefer google's api error response style .

So my service sends error response as json or xml and 400 Bad request error code:

{
  "status": "INVALID_REQUEST",
  "type": "ERROR_MSG",
  "data": {
    "request": "/v2/data?age=23d",
    "errors": [
      "age: not a number"
    ]
  },
  "time": -1
}

otherwise 200 and corresponding message

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM