Matthew
Matthew

Reputation: 10444

What Http response code should I return for validation-only API methods?

I have a domain model of a graph. When a user tried to modify it a cyclic check is performed and the appropriate http response code is returned.

However, we want to build a proposed change on the client and just check whether or not it's valid to the current state of the graph.

The code is simple enough. Just let the user POST the graphChangesToValidate to some validation method and my back-end service which already does this work can confirm whether or not it would be ok to make the changes...

My question is what HTTP status code(s) should I return in this case?

200 would imply that the proposed change passed validation. For failure I don't know if 400 is appropriate because the request is formatted fine and the server is just saying "I understood your request, the answer is no."

I'm thinking that as long as the validation method completes I should return a 200 with either a true or a false boolean in the content.

Is there a more standard way to handle this?

Upvotes: 2

Views: 2375

Answers (2)

Alex
Alex

Reputation: 6047

The status-codes usually don't give information about the content, but just that the request/response was successful according to the protocols (or not).

A (200) status code is part of the http header , what you are talking bout sounds like message content to me.

Having said that, you can use/abuse it any way you want as long the server and client agree on how to handle it. But it would be better to have some useful message that makes sense to other developers (and some API documentation).

Upvotes: 2

sanpaco
sanpaco

Reputation: 815

The answer is... maybe. I personally have typically used a 200 with additional details in the response such as an error code with details of what validation steps failed. There are http codes in the 400 range that could also be used appropriately for validation errors.

Check this issue out for some good comments and discussion on it: Is returning HTTP 409 appropriate for a validation check?

I think that generally as long as the request is appropriately formatted and received, a 200 code should be used and then the business logic is what handles the validation error. If there is an error in formatting, then you could use a HTTP error as a response.

Upvotes: 1

Related Questions