What should a JSON service return on failure / error

后端 未结 11 1114
温柔的废话
温柔的废话 2020-12-02 05:32

I\'m writing a JSON service in C# (.ashx file). On a successful request to the service I return some JSON data. If the request fails, either because an exception was thrown

11条回答
  •  -上瘾入骨i
    2020-12-02 06:35

    Yes, you should use HTTP status codes. And also preferably return error descriptions in a somewhat standardized JSON format, like Nottingham’s proposal, see apigility Error Reporting:

    The payload of an API Problem has the following structure:

    • type: a URL to a document describing the error condition (optional, and "about:blank" is assumed if none is provided; should resolve to a human-readable document; Apigility always provides this).
    • title: a brief title for the error condition (required; and should be the same for every problem of the same type; Apigility always provides this).
    • status: the HTTP status code for the current request (optional; Apigility always provides this).
    • detail: error details specific to this request (optional; Apigility requires it for each problem).
    • instance: URI identifying the specific instance of this problem (optional; Apigility currently does not provide this).

提交回复
热议问题