我知道这是有争议的,但是什么是正确的 HTTP 方法,它只接受输入并执行逻辑并返回响应。
例如:如果我必须公开一个 REST 端点,它接受一个整数并返回一些数字系列?
最佳答案
正如 HTTP 协议(protocol)的 RFC ( https://www.w3.org/Protocols/rfc2616/rfc2616-sec9.html ) 中所述,HTTP 方法可以是幂等的,也可以不是:
Methods can also have the property of "idempotence" in that (aside from error or expiration issues) the side-effects of N > 0 identical requests is the same as for a single request. The methods GET, HEAD, PUT and DELETE share this property. Also, the methods OPTIONS and TRACE SHOULD NOT have side effects, and so are inherently idempotent.
因此,如果您的逻辑显着改变系统状态 - 您最好使用非幂等方法 - POST
。如果通过调用服务方法在系统中的所有更改仅记录到日志文件 - 使用安全的 HTTP 方法,例如 GET
。
关于rest - 用于逻辑执行的正确 REST 方法,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/48612591/