The current project API wants to use restful style, and the current API interface return specification: all successful interfaces return {status:200,msg:'',data:{}}, and error interfaces return {status:403,msg: '',data:{}}, that is, there are only two statuses: 200 and 403. I felt something was wrong, so I searched online
200 OK - [GET]: The server successfully returns the data requested by the user. The operation is idempotent.
201 CREATED - [POST/PUT/PATCH]: The user successfully created or modified data.
202 Accepted - [*]: Indicates that a request has entered the background queue (asynchronous task)
204 NO CONTENT - [DELETE]: The user deleted the data successfully.
400 INVALID REQUEST - [POST/PUT/PATCH]: There is an error in the request issued by the user. The server does not create or modify data. This operation is idempotent.
401 Unauthorized - [*]: Indicates that the user does not have permission (the token, username, and password are incorrect).
403 Forbidden - [*] Indicates that the user is authorized (as opposed to the 401 error), but access is prohibited.
404 NOT FOUND - [*]: The request issued by the user is for a record that does not exist, and the server did not perform the operation. The operation is idempotent.
406 Not Acceptable - [GET]: The format requested by the user is not available (for example, the user requested JSON format, but only XML format).
410 Gone -[GET]: The resource requested by the user has been permanently deleted and will not be obtained again.
422 Unprocesable entity - [POST/PUT/PATCH] A validation error occurred while creating an object.
500 INTERNAL SERVER ERROR - [*]: A server error occurred and the user will not be able to determine whether the request made was successful.
Do I need to follow this specification to return status codes according to different situations? For example, a get request returns 200 successfully, and a post request returns 201 successfully? Instead of just returning 200 on success? By the way, are these status codes really written in the return value? Shouldn't it be determined automatically in the http protocol? Or modify it in the returned header? It's very confusing here, I hope someone can clear it up!
There are two methods
1.status_code is all 200, the response body is as follows:
成功
失败
2. Process according to different status_code (recommended)
成功(2xx)
and return the data directly without additional packaging失败(4xx,5xx)
, return errcode and errmsg{status:200,msg:'',data:{}}
This should be the return result written by you in the API. For example, the servlet returns a json string containing these 3 fields. In other words, the front end can only read this information when your api returns successfully. If the request fails and cannot be read, you can try requesting a wrong URL from the front endIn fact, if it is standardized, it should be returned like this
header('HTTP/1.0 401 Unauthorized');
header('HTTP/1.0 403 Forbidden');
However, in actual situations, many people will not be so standardized.
Even GET requests and POST requests are being mixed, so how can we talk about standards?
Originally, it should be done according to the regulations, but domestic telecom operators may hijack the redirect. For example, if you return 404, the operator may redirect you to their own navigation website, which would be embarrassing.
The data returned by your interface and the http response are two different things
The code returned by the interface is predetermined. You can set it however you want.
The information you are checking is the status code of the http response and has nothing to do with the return from your interface
Business error code 4xx
Success 200 - 204
System error 5xx