Conan
Conan

Reputation: 2358

HTTP response for validation request

I've got a service that validates names, that can be used to check whether a username is OK. It looks something like this:

POST http://names.myservice.com/validate 
content-type: application/json
{"name": "Abdul Hideo McDodgycharacter¬§(*&^$%£!"}

=> 200 OK 
{"errors": "contains invalid characters"}

So the point of the service is to check the validity of a proposed username, also checking in my db to see whether it's already been taken. My question is: should the response code be 400 (Bad Request) when there are validation errors?

If I was building a user API to create users, that's what I'd do when presented with Abdul here, but I'm not. In this case the request is for validation, the input data is acceptable, and the response contains the requested representation, which is a list of errors for the supplied data, so a 200 OK feels right. A 400 would indicate my validation request was malformed, and the data to be validated couldn't be identified.

I realise that this isn't very RESTful, because "validate" is basically a verb, so if there's another way to do this that solves my query, please suggest it!

Upvotes: 1

Views: 200

Answers (1)

Eric Stein
Eric Stein

Reputation: 13672

If the request for validation was successful, then 200 (OK) is the correct response code. As far as endpoints, you can consider

POST /validated-names

Upvotes: 3

Related Questions