I’m developing an add-on that uses the Submissions and Submissions Validation endpoints of the REST API in JavaScript.
I’ve noticed that if the entire form is valid, the response is set to status 200, and if it’s invalid, the response is processed correctly but returns a status of 400! Why did you do this? Can someone explain to me if this really makes sense?
The response should always return a status of 200 to indicate that the request has been processed correctly, regardless of whether the form is valid or invalid IMO. In addition, there’s this information in the response and the “is_invalid” entry, which is all you need to know about the status of the form.
Also, retrieving a 400 causes an error log to appear in the console, making it counter-productive to display real errors.
REST API v2 inherited the 400 status code from the v1 endpoint. I can’t comment on why that status code was used by v1 because that’s from before my time. The 400 status has been used for a decade at this point, so it’s unlikely to be changed due to the risk it could break existing integrations.
You could use the WordPress rest_post_dispatch filter to change the status code. I haven’t tested this, but something like the following should work: