When developing or integrating websites with AnQiCMS, we often deal with API interfaces. When handling the return results of these interfaces,codeandmsgThese fields are almost indispensable parts of all response data.They play the role of "traffic lights" and "instructions", helping us quickly understand the processing 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 function is to simply inform us of the overall status of the API request.It can be imagined as a machine-readable signal, and the program will quickly determine the next step to take based on this number.

The most core in the API design of AnQi CMS,codeis0When you see in the API response,code: 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 returned.code: 0This is a clear success sign, you can proceed with confidence.dataThe specific content returned in the field.

However,codeThe value is not0When, it usually indicates that the request encountered a problem during processing. The Anqi CMS defines some common non-zerocodevalues, each representing a different type of error:

  • -1: General error indication.Whencoderesponse for-1When this occurs, it indicates an error, but it could be of various kinds. At this point, we need to checkmsgField to get more specific error reasons. It's like a 'see the instructions' hint, telling you the details are inside.msginside.
  • 1001: Not logged in.This code is very clear, indicating that the current operation requires the user to log in before proceeding, but the system detects that the user is not logged in or the login credentials have expired.Encounter this situation, usually requires prompting the user to log in again.
  • 1002: Unauthorized.With1001is similar,1002It is 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 may be due to user group restrictions, insufficient roles, and other reasons.
  • 200: API request OK.It is worth noting that in the list of error codes of some APIs,200也被列为“API 请求 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.code: 0. Therefore,200May be more used to indicate the success of the basic HTTP request, while the actual success or failure of business operations, we should still take0as the standard. In actual development, it is usuallycode: 0As the final judgment basis for the success of business operations.

msgField: Detailed explanation of the message describing the request result.

msgField is a string type value, it is used ascodeField supplement description, providing more readable information. Its main value lies in providingcodeThe field represents a specific human-readable description of the state, whether successful or failed.

When the request is successful (code: 0)msgThe field may be an empty string, indicating that the operation was successfully completed without any special information to emphasize. However, it can also contain friendly success messages. For example, after the document is successfully 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 the operation result on the user interface.

When the request fails (codeNot to0)msgthe importance of the field becomes more prominent. It will detail-1The specific cause of this general error code, such as "the specified document not found1001or1002such specific error codes,msgThe field will also emphasize its meaning, such as "User not logged in" or "Insufficient permissions".These specific error messages are critical for developers to diagnose issues, log events, and provide accurate feedback to users.

Actual application: How to use it togethercodeandmsg

When handling the API response of an enterprise CMS, **practice is always to check firstcodefields. IfcodeYes0If so, it means the request has succeeded, you can safely parse and use itdataField data, and display as needed.msgSuccessful prompt.

IfcodeIt is not0so thatmsgField becomes the 'key' for you to understand and handle errors. At this time, you should read 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 combined usecodeandmsgThe way, can greatly improve the robustness and user experience of the application.


Common Questions and Answers (FAQ)

1. Why sometimescodeYes0ButmsgThe field is empty?Whencoderesponse for0andmsgThis indicates that the API request operation is completely successful when the field is empty, and the server believes there is no additional message to send to the client. This is a normal phenomenon in many interfaces, such as after successfully obtaining a list of data or details of a document, the main focus is ondataThe field itself, without the need for specific textual success prompts.

2.codeYes200and0Do all of them mean success? What are the differences?In the API response of Anqi CMS,code: 0Expresses clearlyBusiness operation successful.code: 200出现在一些API的“错误代码”列表中,通常指EnglishHTTP请求本身成功English。在实际应用中,您应该以Englishcode: 0As a judgment standard for business logic operations. IfcodeYes200But businesscodeIt is not0, this means that the request has reached the server but the business processing has failed.

3. WhencodeYes-1What should I do?Whencoderesponse for-1It is a general error code, meaning the operation failed. In this case, the most critical thing isCheckmsgField.msgThe field will provide specific error reason descriptions, such as "invalid parameters", "document does not exist", etc. You should be able tomsgThe content is displayed to the user or recorded in the log for debugging and troubleshooting.