+ 1

HTTP Response Status Code

I have a form to create new user in my MySQL database. I'm using fetch API to send the form data to a PHP script without reloading the page. The script will do the task using the form data. If something goes wrong in the script (e.g. user is not created due to backend form validation failure) I want to change the response status code. Is it okay to do so? If yes which response status code should I use? Is there any better way to do the same? I just need to know if the user account has been created or not. Based on this I will show alert message.

5th Dec 2022, 8:12 PM
Kashyap Kumar
Kashyap Kumar - avatar
4 Respostas
+ 3
Like KrOW said. It's easier to always return status code 200 and include the actual status code in the body. This way its easier to use on the client side since you dont have to catch if the request succeed or not. It gets annoying if you have many endpoints and you're have to specify atleast two callbacks. This way you can only focus on the returned body. However the "right" way would still be to change the status code also in the response header. Google "http response codes" to see the right codes you need
7th Dec 2022, 9:25 PM
Toni Isotalo
Toni Isotalo - avatar
+ 2
There are few "standard" approach to this... Personally i use http code about http protocol while the response content about logic status. At example i usually return always the http status code 200 but the content return a json with the format: { status: 0, description: "OK" } where status prop is an logical applications status code and description is a description of the status useful to the client. For example, in you case of the failure of the validation, the json can be: { status: 1, description: "Input validation failed" }
5th Dec 2022, 9:20 PM
KrOW
KrOW - avatar
+ 2
Hey there! It's great that you're using the Fetch API to create a new user in your MySQL database. When it comes to handling different scenarios and response statuses, you're on the right track! Changing the response status code is absolutely okay and can be a helpful approach to signaling different outcomes of your script execution. Here are a few suggestions for the response status codes you can consider: 200 OK: If the user is successfully created, you can respond with a status code of 200 OK. This indicates that the request was successful, and the user account has been created. 400 Bad Request: If the backend form validation fails or the provided data is invalid, using a 400 Bad Request status code is appropriate. This signals that the client's request is malformed or invalid. 422 Unprocessable Entity: This is another suitable status code for validation failures. It indicates that the server understands the content type of the request entity and the syntax of the request entity is correct, but it was unable to process the contained instructions. To display an alert message based on the outcome, you can use the Fetch API's .then() method to access the response status code and handle different scenarios accordingly. Here's a basic example using JavaScript: fetch('your-php-script-url', { method: 'POST', body: formData }) .then(response => { if (response.ok) { // User created successfully, show success message // You can also consider parsing the response JSON if needed } else if (response.status === 400) { // Validation failure, show error message for bad request } else if (response.status === 422) { // Validation failure, show error message for unprocessable entity } else { // Handle other error scenarios } }) .catch(error => { // Handle fetch error }); This approach is clean and provides clarity regarding the outcome of your script execution, allowing you to present appropriate messages to the user. Good luck with your project! 🚀👩‍💻👨‍💻
22nd Aug 2023, 6:13 PM
Ali Saeed Thabt
Ali Saeed Thabt - avatar
+ 1
Thanks KrOW and Toni.
8th Dec 2022, 8:42 AM
Kashyap Kumar
Kashyap Kumar - avatar