When using AnQiCMS for website development or integration, we often deal with API interfaces. When handling the return results of these interfaces,codeandmsgThese fields are almost indispensable in all response data.They play the role of 'traffic lights' and 'instruction manuals', helping us quickly understand the result of the request, whether it is successful or not.
codeField: Quickly judge the status code of the request result
codeThe field is an integer type value, its main role is to inform us succinctly about the overall status of the API request.Imagine it as a machine-readable signal, the program will quickly determine the next step based on this number.
In the API design of AnQi CMS, the most corecodeValue is0. When you see in the API responsecode: 0This means that your request has been successfully processed by the server and the operation has been completed as expected. For example, when you successfully retrieve document details, publish content, or query list data, it is usually returnedcode: 0This is a clear success sign, you can continue to process with confidence.dataThe specific content returned in the field.
However, whencodeThe value is not0When, it usually indicates that the request encountered a problem during processing. Anqicms defines some common non-zerocodevalues, each representing a different type of error:
-1: General error indication.WhencodeWith-1When, it indicates an error, but this error may be diverse. At this time, we need to checkmsgField to get more specific error reasons. It's like a 'see instructions' prompt, telling you the details are inmsgit.1001: Not logged in.This code is very clear, indicating that the operation requires the user to log in before proceeding, but the system has detected that the user has not logged in or the login credentials have expired.Encounter this situation, usually need to guide the user to log in again.1002: Unauthorized.with1001similar,1002It is also related to user permissions, but it indicates that although the user may be logged in, their account does not have sufficient permissions to perform the requested operation.This could be due to user group restrictions, insufficient roles, and other reasons.200: API request OK.It is worth noting that in some API error code lists,200It is also listed as "API Request OK". This is indeed a successful status code in the HTTP protocol, indicating that the request itself has reached the server and has been processed.But in the business logic of AnQi CMS, if the business operation is successful, it will usually returncode: 0. So,200It is more likely to be used to indicate the success of the underlying HTTP request, while whether the actual business operation is successful or not, we should still take0as the standard. In actual development, it is usually withcode: 0The final judgment basis for the success of business operations.
msgField: Detailed explanation of the message describing the request result.
msgThe field is a string type value, which acts ascodeField supplement explanation, provides more readable information. Its main value lies incodeThe field represents a specific human-readable description of the status, whether it is successful or not.
When the request is successful (code: 0)msgThe field may be an empty string, indicating that the operation was successfully completed without any special emphasis. However, it can also contain friendly success messages. For example, after the document is published,msgIt may be 'Published successfully'; after the payment interface is successfully processed,msgIt may be 'Payment successful'. This information is very helpful for displaying operation results in the user interface.
When the request fails(codenot0)msgthe importance of the field becomes more prominent. It will specify in detail-1The specific cause of this general error code, such as 'The specified document not found', 'Request parameter error', 'Insufficient inventory', etc., for1001or1002such specific error codes,msgThe field will also emphasize its meaning, such as 'User not logged in' or 'Insufficient permissions'.This specific error information is crucial for developers to diagnose problems, log records, and provide accurate feedback to users.
How to combine usage in practicecodeandmsg
In handling the Anqi CMS API response, **practice is always to check firstcodefield. IfcodeIs0This means the request has been successful, you can safely parse and use itdataThe data in the field is displayed as neededmsgThe success prompt is displayed.
Ifcodeis not0thenmsgThe field becomes the 'key' for you to understand and handle errors. At this point, you should read it first.msgThe content, to determine what type of error occurred. According tomsgThe specific information provided, your application can perform corresponding error handling logic, such as prompting the user to log in, informing of insufficient permissions, displaying friendly error messages, or recording detailed error logs for subsequent troubleshooting. This combination is usedcodeandmsgThe way, can greatly improve the robustness and user experience of the application.
Frequently Asked Questions (FAQ)
1. Why sometimescodeIs0ButmsgThe field is empty?WhencodeWith0andmsgWhen the field is empty, this indicates that the API request operation was completely successful, and the server believes that there is no additional message that needs to be sent to the client. This is a normal phenomenon in many interfaces, such as after successfully obtaining a data list or document details, the main focus is ondataField itself, without any specific textual success prompt.
2.codeIs200and0Do all of them mean success? What are the differences?In the AnQi CMS API response,code: 0Expressly indicatesBusiness operation successfulHowevercode: 200Appear in the error code list of some APIs, usually refers toThe HTTP request itself was successfulIn practical applications, you should usecode: 0As a criterion for the successful operation at the business logic level. IfcodeIs200But businesscodeis not0This means that the request has reached the server but the business processing has failed.
3. WhencodeIs-1What should I do?WhencodeWith-1When, it is a general error code, meaning the operation failed. In this case, the most critical isCheckmsgfield.msgThe field will provide specific error reason descriptions, such as 'Parameter is invalid', 'Document does not exist', etc. You should convertmsgThe content is displayed to the user or recorded in the log for debugging and problem troubleshooting.