RESTful API:什么 METHOD/HEADER 组合仅用于验证 [英] RESTful API: What METHOD/HEADER combo to use for validation-only
问题描述
我希望我的 API 具有仅验证请求.例如,如果我有一个 URL,例如:
I would like my API to have a validation-only request. For example, if I have a URL such as:
http://api.somesite.com/users/12345
并且用户正在填写有关客户端的信息表单,我最终将对该资源进行 PATCH/PUT/POST.当用户填写表单时,我可能希望定期将他们部分完整的更新表示发送到服务器,以便我可以显示他们输入的实时验证(例如,那个用户名已经被占用了",那个密码是太短了").
and the user is filling out a form of information on a client that I will eventually PATCH/PUT/POST to that resource. As the user is filling out the form, I might want to send over their partially-complete updated representation over to the server periodically so I can display realtime validation of their input (e.g., "That username is already taken", "That password is too short").
似乎没有标准的 HTTP 方法或 HEADER 允许在同一资源上执行此行为.看来我的选择是:
There isn't a standard HTTP METHOD or HEADER that seems to allow for this behavior on that same resource. It seems my options are:
- 为验证创建一个新的从属资源
- 使用自定义标头(x-somesite-validation-only)和 PUT 表示我想验证但不保存
推荐答案
一些选项
1) 使用自定义标题
2)在查询字符串中放入一些内容,指示仅验证
3) 使用动作 URl 例如\IndividualClient\123\actions\Validate\Invoke {section 19 here http://restfulobjects.files.wordpress.com/2011/11/restful-objects-spec-052.pdf}
4) 分层 URL,例如\IndividualClient\123\验证
1) Use custom header
2) Put something in the query string indicating to validate only
3) Use Action URl e.g. \IndividualClient\123\actions\Validate\Invoke {section 19 here http://restfulobjects.files.wordpress.com/2011/11/restful-objects-spec-052.pdf}
4) Hierarchical URL e.g. \IndividualClient\123\Validation
从这个帖子我找到了这个建议
From this post I find this advice
当你必须做一些感觉像 RPC 的事情时,请使用 POST 做使用 GET 进行诸如计算之类的事情,除非您的输入很大,在哪种情况使用POST
Do use POST whenever you have to do something that feels RPC-like Do use GET for things like calculations, unless your input is large, in which case use POST
关于您的具体问题,#4 和 #5 应该使用 POST.这些操作属于上述类似 RPC"的指导方针.对于 #5,请记住 POST 不一定必须>使用 Content-Type:application/x-www-form-urlencoded.这可以很容易地成为 JSON 或 CSV >有效负载.
With regard to your specific question, POST should be used for #4 and #5. These operations fall >under the "RPC-like" guideline above. For #5, remember that POST does not necessarily have to >use Content-Type: application/x-www-form-urlencoded. This could just as easily be a JSON or CSV >payload.
这是我正在考虑的:
这是一个资源的添加:
用户/验证
发布
请求:用户资源
响应:验证结果
响应代码 200、400、404、500
This is the add of a resource :
user/validation
POST
Request:UserResource
Response:ValidationResult
Response Codes 200, 400. 404. 500
这是一个资源的更新
用户/204/验证
发布
请求:用户资源,
响应:验证结果响应代码 200、400、404、500
This is the update of a resource
user/204/validation
POST
Request:UserResource,
Response:ValidationResult
Response Codes 200, 400. 404. 500
这篇关于RESTful API:什么 METHOD/HEADER 组合仅用于验证的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!