在研究用于异步操作的RESTful API时,我遇到了以下设计模式:
POST uri:longOperation
返回:
GET uri:pendingOperation
返回:
GET uri:operationResponse
我觉得最后一步有问题。考虑如果异步操作使用对HTTP GET
没有意义的错误代码完成会发生什么,例如HTTP 409 ("Conflict")
。
HTTP 303
指向与 uri:pendingOperation 相关的响应,而不是 uri:operationResponse ?HTTP 303
被视为有害吗?如果没有,为什么?答案 0 :(得分:6)
是否需要HTTP 303指向与uri相关的响应:pendingOperation而不是uri:operationResponse?
spec没有明确说明这是必需的,但我倾向于同意你的意见。
以这种方式使用HTTP 303是否有害?如果没有,为什么?
我认为你通过做一个303就失去了能力。虽然完成后自动重定向是“好的”,但它使得你没有机会在结果周围提供元数据,这可以被利用报告等...还有许多客户端不会自动关注303,所以客户端可能需要做的工作才能遵循303 Location标题。
这是我们能做的最好的,还是有更好的方法?
我倾向于建议让GET uri:pendingOperation
返回200,状态资源总是在输出“完成”时引用输出。像
不完整时
{
"status" : "PENDING"
}
错误时
{
"status" : "COMPLETE"
"errors" : [
{
"typeId" : "OPERATION_TIMEOUT",
"description" : " "The request was unable to complete because the systems are unresponsive".
}
]
}
成功时
{
"status" : "COMPLETE"
"links" : {
"result" : {
"href" : "http://api.example.com/finished-resource/1234",
}
]
}