Warning: file_get_contents(/data/phpspider/zhask/data//catemap/5/url/2.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Rest 具有复合键的GET请求的良好URL语法_Rest_Url - Fatal编程技术网

Rest 具有复合键的GET请求的良好URL语法

Rest 具有复合键的GET请求的良好URL语法,rest,url,Rest,Url,让我们在我的REST API中获取以下资源: GET `http://api/v1/user/users/{id}` 在正常情况下,我会这样使用: GET `http://api/v1/user/users/aabc` 其中,aabc是用户id 但是,有时我不得不设计REST API,以便通过ID传递一些额外的信息。例如: GET `http://api/v1/user/users/customer:1` GET `http://api/v1/user/users/customer:1;t

让我们在我的REST API中获取以下资源:

GET `http://api/v1/user/users/{id}`
在正常情况下,我会这样使用:

GET `http://api/v1/user/users/aabc`
其中,
aabc
是用户id

但是,有时我不得不设计REST API,以便通过ID传递一些额外的信息。例如:

GET `http://api/v1/user/users/customer:1`
GET `http://api/v1/user/users/customer:1;type:agent`
其中
customer:1
表示我正在使用客户域中的id查找用户,该id为1

我现在有一个场景,其中标识符是多个键(复合键)。例如:

GET `http://api/v1/user/users/customer:1`
GET `http://api/v1/user/users/customer:1;type:agent`
我的问题:在上面的URL中,我应该使用什么作为
customer:1
type:agent
之间的分隔符

据我所知,分号是不允许的。

您应该:

使用参数: 获取
http://api/v1/user/users?customer=1

或使用新的URL: 获取
http://api/v1/user/users/customer/1

但要使用像这样的标准


(“一般来说,路径倾向于缓存,参数倾向于不缓存。”)

与其尝试创建一个通用结构,以便一次通过多个键访问记录,我建议尝试更多地根据具体情况考虑这一点

以您的示例为例,一种解释方法是您有多个客户,这些客户可能都有多个用户帐户。这方面的自然层次结构是:

/customer/x/user/y

通常可以做出这样一个优雅的决定,不仅可以解决问题,而且还可以通过一对多的关系轻松地记录您的数据模型。

为什么不使用查询参数,例如
/v1/users?customerId=1&type=agent
/user/users
似乎可疑,构建自己的表示通常不是一个好主意。请尝试对URL进行编码。我正在考虑这个问题,我认为这个问题更多的是关于当您的资源id是复合键时该怎么做