在 REST 调用的输入参数中放入什么

What to put in the input argument of a REST call

我正在与一位开发人员发生争执,需要一些外部视角。 定义 REST Api 时,通常将什么作为输入参数?

假设我们有以下调用 GetCarByModelName,您是否希望它接受一个输入,即 ModelName(字符串、枚举,等等)或 ModelName 在字段上的整个 Car 对象,然后是内部工作函数的一部分将查看 ModelName 并忽略所有其他信息(如果提供)?

1. GetCarByModelName(ModelName) 
2. GetCarByModelName(Car)

Car
{
  Type : string
  Weight : int
  ProductionYearStart : DateTime
  ProductionYearStop : DateTime
  Price : Decimal
  ModelName : string
  BrandName : string
}

首先让我们解决一些问题 - HTTP GET 并发送一个 JSON 正文(这看起来就像您打算做的那样)。

author of REST himself commented here,即:

Server semantics for GET, however, are restricted such that a body, if any, has no semantic meaning to the request. .... So, yes, you can send a body with GET, and no, it is never useful to do so.

在你提到的问题中:

where ModelName is on of the fields and then the internal workings of the function would look at the ModelName and ignore all the other information if its provided?

一般来说,无论是一个字段还是多个字段,您都不应该发送带正文的 HTTP GET(所以您可能会说你们都错了)。

GetCarByModelName 显然具有 HTTP GET 请求的语义。 在一个常见的 REST api 中,有人会期望你有一个 Car 资源,可以通过模型名称查询。但是让我们给它一些背景信息——一辆汽车在汽车经销商店里,用户想要获得 BMW 型号 X5 汽车的列表。这应该看起来像:

HTTP GET /api/cardealer/{carDealerId}/cars?modelName=BMW%20X5