我已經使用了#2自己幾次。比#1好嗎?我認爲這取決於你的API被用於什麼。
我喜歡#2,因爲它給正在測試API的開發人員提供一些測試請求,快速瞭解他在請求中所做的所有錯誤/錯誤,因此他立即知道他必須修復哪些錯誤/錯誤使該請求有效。如果你逐一返回錯誤(如#1),你必須不斷重試請求,並且交叉手指希望這次有效。
但正如我所說#2對開發人員非常有用,但其原因並不適用於最終用戶。最終用戶通常不關心它是如何實施的。軟件是否正在執行返回5個錯誤的1個請求或每個返回1個錯誤的5個後續請求。
只要在客戶端處理得當,最終用戶不應該注意到其中的差異。當然如何處理,當然很取決於客戶端是。
除了加速開發之外,#2(在生產中)的另一個好處是它只需要較少的請求,這當然會降低服務器負載。
I would like to know if anyone went #2 and maybe have any improvements on it so I opened a bounty.
肯定有被做了改進。實際上,可以省略身體中的一些數據。
{
"status": 400,
"error": {
"code": 1 //General bad Request code
"message": "Bad Request",
"developer_message": "Field validation errors."
"more_info": "www.api.com/help/errors/1",
"error_details": {
0: {
"code": 2 // Specific field validation error code
"message": "Field \"x\" is missing from the array structure",
"developer_message": "The request structure must contain the following fields {a,b,c{x,y,z}}",
"more_info": "www.api.com/help/errors/2"
},
1: {
(
"code": 3 // Specific field validation error code
"message": "Incorrect Format for field \"y\"",
"developer_message": "The field \"y\" must be in the form of \"Y-m-d\"",
"more_info": "www.api.com/help/errors/3"
)
}
)
對於HTTP響應,狀態碼不應放在主體中,而應放在標題中。這意味着這裏可以省略"status": 400
和"message": "Bad Request"
。 400應該是響應的狀態碼,400意味着錯誤請求。這是一個HTTP標準,不必在響應中解釋。此外,"developer_message": "Field validation errors."
也是重複的,因爲具體的錯誤已經包含在每個單獨的錯誤中,所以我們可以將其忽略。
這使得
{
"error": {
"code": 1 //General bad Request code
"more_info": "www.api.com/help/errors/1",
"error_details": {
0: {
"code": 2 // Specific field validation error code
"message": "Field \"x\" is missing from the array structure",
"developer_message": "The request structure must contain the following fields {a,b,c{x,y,z}}",
"more_info": "www.api.com/help/errors/2"
},
1: {
(
"code": 3 // Specific field validation error code
"message": "Incorrect Format for field \"y\"",
"developer_message": "The field \"y\" must be in the form of \"Y-m-d\"",
"more_info": "www.api.com/help/errors/3"
)
}
)
"code": 1 //General bad Request code
"more_info": "www.api.com/help/errors/1",
這兩條線沒有真正意義了現在。他們也沒有要求,因爲每個錯誤都有它自己的代碼和信息鏈接,所以我們可能會剝離這些線路爲好,離開這個
{
"error": {
"error_details": {
0: {
"code": 2 // Specific field validation error code
"message": "Field \"x\" is missing from the array structure",
"developer_message": "The request structure must contain the following fields {a,b,c{x,y,z}}",
"more_info": "www.api.com/help/errors/2"
},
1: {
(
"code": 3 // Specific field validation error code
"message": "Incorrect Format for field \"y\"",
"developer_message": "The field \"y\" must be in the form of \"Y-m-d\"",
"more_info": "www.api.com/help/errors/3"
)
}
)
的400個狀態碼已經表示出現了錯誤,所以你不」因爲我們已經知道有錯誤,所以不必再指出"error": {error details}
。錯誤列表可以簡單地成爲根對象:
[
{
"code": 2//Specificfieldvalidationerrorcode
"message": "Field \"x\" is missing from the array structure",
"developer_message": "The request structure must contain the following fields {a,b,c{x,y,z}}",
"more_info": "www.api.com/help/errors/2"
},
{
"code": 3//Specificfieldvalidationerrorcode
"message": "Incorrect Format for field \"y\"",
"developer_message": "The field \"y\" must be in the form of \"Y-m-d\"",
"more_info": "www.api.com/help/errors/3"
}
]
因此,所有現在遺留在身體中的僅僅是一個錯誤列表。
狀態碼在響應標題中指定。
詳細信息在響應主體中指定。
我想知道是否有人去#2,也許有任何改進,所以我打開了賞金。 – Ski
此API用於什麼以及錯誤消息的目的是什麼?這些消息是否會顯示給最終用戶?每秒/分鐘/每天預計有多少個請求?沒有這些信息,你的問題的答案就不可能是準確的。你沒有答案,因爲這個問題太廣泛了,這取決於API的使用。 – skobaljic