API Response Codes, Error Handling, and Best PracticesZid API is built to adhere strictly to REST principles, ensuring seamless communication between clients and our server. Below is a detailed guide to understanding the various HTTP response codes, error handling mechanisms, and best practices to help developers efficiently interact with our API.
When a request fails due to client-side issues, the API will return a 4xx error code. Understanding these codes is crucial for diagnosing problems quickly.
Color Code
Response
Slug
Status
Meaning
Troubleshooting Tips
400
bad_request
Bad Request
The server could not understand the request due to invalid syntax.
Check the request syntax and parameters. Ensure all required fields are included.
401
unauthorized
Unauthorized
Authentication is required and has failed or has not yet been provided.
Verify API keys or authentication credentials.
403
forbidden
Forbidden
The server understood the request but refuses to authorize it.
Ensure the user has the necessary permissions.
404
not_found
Not Found
The requested resource could not be found.
Double-check the URL and resource ID.
405
method_not_allowed
Method Not Allowed
The method specified in the request is not allowed.
Verify that you are using the correct HTTP method (GET, POST, etc.).
406
not_acceptable
Not Acceptable
The server cannot produce a response matching the list of acceptable values defined in the request's headers.
Adjust the Accept headers in your request.
410
gone
Gone
The resource requested is no longer available and will not be available again.
This resource has been permanently removed, and you should clean up references to it.
422
validation_failed
Unprocessable Entity
The server understands the content type and syntax of the request entity, but it was unable to process the contained instructions.
Review the data being submitted for missing fields or incorrect formats.
429
too_many_requests
Too Many Requests
You have sent too many requests in a given amount of time.
When the request is invalid, you might receive a response like this:
{"status":422,"success":false,"error":{"code":"validation_failed","message":"Invalid input data.","fields":{"email":["Email format is invalid."],"password":["Password must be at least 8 characters."]}}}
When a request results in multiple errors leading to a 4xx response, our API returns a structured response that includes a list of affected fields and an array of associated error messages. This helps in pinpointing the exact issues for each field.
{"status":422,"success":false,"error":{"code":"خطأ في التحقق","message":"تحتوي بعض الحقول على بيانات غير صحيحة.","fields":{"اسم_المستخدم":["اسم المستخدم مطلوب."]}}}
{"status":422,"success":false,"error":{"code":"خطأ في التحقق","message":"تحتوي عدة حقول على بيانات غير صحيحة.","fields":{"اسم_المستخدم":["اسم المستخدم مطلوب."],"البريد_الإلكتروني":["صيغة البريد الإلكتروني غير صحيحة."],"كلمة_المرور":["يجب أن تتكون كلمة المرور من 8 أحرف على الأقل."],"تأكيد_كلمة_المرور":["تأكيد كلمة المرور غير متطابق."]}}}