RESTful API:仅用于验证的方法/标头组合 [英] RESTful API: What METHOD/HEADER combo to use for validation-only

查看:107
本文介绍了RESTful API:仅用于验证的方法/标头组合的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我希望我的API发出仅验证请求.例如,如果我有一个网址,例如:

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 METHOD或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:

  1. 创建新的下属资源进行验证
  2. 使用自定义标头(仅x-somesite-validation)和PUT指示我要验证但不保存

推荐答案

某些选项

1)使用自定义标头
2)在查询字符串中放入一些内容,以指示仅进行验证
3)使用动作URl,例如\ IndividualClient \ 123 \ actions \ Validate \ Invoke {第19节,此处

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

关于您的特定问题,POST应该用于#4和#5.这些操作>属于上面的类似于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.

这是我正在考虑的:

这是资源的添加:
用户/验证
POST
请求:UserResource
响应:ValidationResult
响应代码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/验证
POST
请求:UserResource,
回应:验证结果 响应代码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:仅用于验证的方法/标头组合的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆