說我有一個資源未找到子實體的正確HTTP狀態碼是什麼?
/Products/123
而且每個Product
在後臺數據庫相關的Supplier
實體。 POST和PUT請求必須指定供應商ID,然後用它從數據庫中提取供應商實體。
如果用戶發出PUT /Products/123
什麼應返回,這是發現,但包括不良供應商ID,這是不?
404 Not Found
帶有指定找不到哪個資源的消息?
409 Conflict
?
說我有一個資源未找到子實體的正確HTTP狀態碼是什麼?
/Products/123
而且每個Product
在後臺數據庫相關的Supplier
實體。 POST和PUT請求必須指定供應商ID,然後用它從數據庫中提取供應商實體。
如果用戶發出PUT /Products/123
什麼應返回,這是發現,但包括不良供應商ID,這是不?
404 Not Found
帶有指定找不到哪個資源的消息?
409 Conflict
?
的404
狀態代碼可能不是正確的選擇,因爲一直沒有找到的資源是不是你要求的目標:
The
404
(Not Found) status code indicates that the origin server did not find a current representation for the target resource or is not willing to disclose that one exists. A404
status code does not indicate whether this lack of representation is temporary or permanent; the410
(Gone) status code is preferred over404
if the origin server knows, presumably through some configurable means, that the condition is likely to be permanent.
的409
狀態代碼可能是適合這種情況,但不是最好的選擇(我不會定義這種情況下作爲衝突):
The
409
(Conflict) status code indicates that the request could not be completed due to a conflict with the current state of the target resource. This code is used in situations where the user might be able to resolve the conflict and resubmit the request. The server SHOULD generate a payload that includes enough information for a user to recognize the source of the conflict. [..]
我會去422
狀態碼有明顯的DES cription在響應有效載荷:
11.2. 422 Unprocessable Entity
The
422
(Unprocessable Entity) status code means the server understands the content type of the request entity (hence a415
(Unsupported Media Type) status code is inappropriate), and the syntax of the request entity is correct (thus a400
(Bad Request) status code is inappropriate) but was unable to process the contained instructions. For example, this error condition may occur if an XML request body contains well-formed (i.e., syntactically correct), but semantically erroneous, XML instructions.
下圖(從this page萃取)是相當有見地的,當涉及到採摘最適宜4xx
狀態碼:
我不相信有一個正確答案這個問題(除非一些REST純粹可以提供一些線索),但我們目前使用(或濫用...)HTTP 400
(錯誤請求)與一個額外的HTTP標題說明錯誤(即X錯誤:供應商ID無效)。但HTTP 422也是一個很好的選擇。因爲是指定的響應是關於子資源沒有明確的方法 以下狀態404或409將會造成混亂。
將404與一個消息有效載荷一起返回,表明類似「沒有找到ID爲999的供應商」,從而消除混淆也是可以接受的嗎? – BCA
@BCA總是歡迎響應有效負載中的消息,使事情變得清晰。無法找到請求的資源時,「404」是合適的。在這種情況下,請求的資源(ID爲123的產品)存在並且可以找到,但請求有效負載(包含無效數據)存在問題。因此,對錯誤進行很好描述的'422'會很好。 –
你好我會使用404如前所述:
The 404 (Not Found) status code indicates that the origin server did not find a current representation for the target resource or is not willing to disclose that one exists. A 404 status code does not indicate whether this lack of representation is temporary or permanent; the 410 (Gone) status code is preferred over 404 if the origin server knows, presumably through some configurable means, that the condition is likely to be permanent.
因爲你正在尋找存在的產品,但供應商ID沒有,所以基本上就像是我們在不同的城市找你,你的存在,但不是在城市,所以我們會說,嘿,我們做了找不到你。
我相信供應商和產品他們有一種關係,這是一種很難的關係,如果你沒有該產品的供應商,產品就不存在,所以這意味着你不能更新產品不知道它是供應商。
你有沒有考慮'422'? –