2
votes

I have to create a couple of web services for validating possible values of given fields. I'm contemplating having something like:

POST /entity/fieldName body { fieldValues }

where the POST will return 400 (Bad request) if the arguments are invalid and 422 (Unprocessable entity) otherwise. However I do not really like the 422 response part very much since it makes the request always return an error. On the other hand since I'm only doing validation and this is a POST I don't want to actually create a new resource on the server (i.e. return 200). Is there another HTTP method / API endpoint that is better suit for this? For what it's worth I will be checking that the entity field with <fieldName> has its value in a given range.

2
Well, as the endpoint is for validating purposes in my opinion it should return 422 Unprocessable Entity if the entity is not valid (instead of 400 bad request) and 200 OK if the entity is valid.BackSlash
@BackSlash, but doesn't using POST and returning 200 imply that a new resource has been created on the server? Because nothing like this will actually happen.asenovm
I think that "A new resource has been created" is 201 Created. 200 OK means "Request was processed and everything was ok" to me. But I'm definitely not a REST expert, so let's wait for others' answers/commentsBackSlash
@BackSlash, thanks! You are right, I think - I popped into this stackoverflow.com/questions/1860645/….asenovm

2 Answers

1
votes

If all you do is validating, then I think you should send 422 by validation error and 200 by validation success. The POST does not mean you have to always create a new entity.

The action performed by the POST method might not result in a resource that can be identified by a URI. In this case, either 200 (OK) or 204 (No Content) is the appropriate response status, depending on whether or not the response includes an entity that describes the result.

If a resource has been created on the origin server, the response SHOULD be 201 (Created) and contain an entity which describes the status of the request and refers to the new resource, and a Location header (see section 14.30).

1
votes

I'm prefer google's api error response style.

So my service sends error response as json or xml and 400 Bad request error code:

{
  "status": "INVALID_REQUEST",
  "type": "ERROR_MSG",
  "data": {
    "request": "/v2/data?age=23d",
    "errors": [
      "age: not a number"
    ]
  },
  "time": -1
}

otherwise 200 and corresponding message