API:数组参数应该使用哪个动词

API: Which verb should we use with array parameters

例如,我们需要一个 API 方法,它获取数组作为输入(设备 ID 数组)和这些设备的 returns 列表(每个设备的完整信息)。

所以,GET方法的所有属性都有,但是在url中传递是个坏主意(数组可以很大),所以,最好在body中传递:

    [HttpGet("api/deviceNames")]
    [ProducesResponseType(typeof(List<DeviceInfo>), StatusCodes.Status200OK)]
    public IActionResult GetDeviceNamesByIds([FromBody]List<string> deviceIds)
    {
        var d = _deviceService.GetDevicesByIds(deviceIds);

        return Ok(d);
    }

它工作正常。但是许多作者不建议将正文用于 GET 方法,例如https://groups.yahoo.com/neo/groups/rest-discuss/conversations/messages/9962?guccounter=1

Yes. In other words, any HTTP request message is allowed to contain a message body, and thus must parse messages with that in mind. Server semantics for GET, however, are restricted such that a body, if any, has no semantic meaning to the request. The requirements on parsing are separate from the requirements on method semantics.

So, yes, you can send a body with GET, and no, it is never useful to do so.

This is part of the layered design of HTTP/1.1 that will become clear again once the spec is partitioned (work in progress).

....Roy

但是,从另一边看:

Update The RFC2616 referenced as "HTTP/1.1 spec" is now obsolete. In 2014 it was replaced by RFCs 7230-7237. Quote "the message-body SHOULD be ignored when handling the request" has been deleted. It's now just "Request message framing is independent of method semantics, even if the method doesn't define any use for a message body" The 2nd quote "The GET method means retrieve whatever information ... is identified by the Request-URI" was deleted. - From a comment

RFC 7231 没有改变语义

A payload within a GET request message has no defined semantics; sending a payload body on a GET request might cause some existing implementations to reject the request.

它不能正常工作 - 不是真的。问题在于 HTTP 的缓存语义不考虑请求主体;通用组件将假定服务器尊重标准化语义,因此不会挖掘请求的消息主体以查看请求数组是否与任何先前缓存的表示匹配。

这反过来意味着,为了防止兼容组件出现问题,您将不得不禁用资源缓存。

由于缓存是 REST architectural style 中的一个重要约束,这显然是朝着错误的方向迈出了一步。

如果您必须使用负载,那么您可能应该查看 POST,而不是 GET。另请参阅:SOAP、GraphQL。

But POST is not idempotent at all

没错。需要做出权衡。