简体   繁体   English

用于字段验证的 REST api

[英]REST api for field validation

I have to create a couple of web services for validating possible values of given fields.我必须创建几个 Web 服务来验证给定字段的可能值。 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.如果参数无效,POST 将返回 400(错误请求),否则返回 422(不可处理实体)。 However I do not really like the 422 response part very much since it makes the request always return an error.但是我不太喜欢 422 响应部分,因为它使请求总是返回错误。 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).另一方面,由于我只是在做验证并且这是一个 POST,我不想在服务器上实际创建一个新资源(即返回 200)。 Is there another HTTP method / API endpoint that is better suit for this?是否有另一个更适合此的 HTTP 方法/API 端点? For what it's worth I will be checking that the entity field with <fieldName> has its value in a given range.对于它的价值,我将检查带有<fieldName>的实体字段是否在给定范围内具有其值。

If all you do is validating, then I think you should send 422 by validation error and 200 by validation success.如果您所做的只是验证,那么我认为您应该发送422验证错误和200验证成功。 The POST does not mean you have to always create a new entity. POST 并不意味着您必须始终创建一个新实体。

The action performed by the POST method might not result in a resource that can be identified by a URI. POST 方法执行的操作可能不会产生可由 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.在这种情况下,200(正常)或 204(无内容)是适当的响应状态,具体取决于响应是否包含描述结果的实体。

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).如果在源服务器上创建了资源,则响应应该是 201(已创建)并包含一个实体,该实体描述请求的状态并引用新资源,以及一个位置标头(参见第 14.30 节)。

I'm prefer google's api error response style .我更喜欢谷歌的 api 错误响应风格

So my service sends error response as json or xml and 400 Bad request error code:所以我的服务将错误响应作为 json 或 xml 和400 Bad request错误代码发送:

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

otherwise 200 and corresponding message否则200和相应的消息

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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