Returning true/false in REST service?
I'm designing a REST service and there's a need for a check to see if an address is correctly entered. What I'm thinking about is how you would design a REST interface for checking if an full street address is valid.
I have this /address service and I could for example do a POST /address/validation
which returns a xml/json true or false, but it seems quite un-REST-ful to me.
Another way would be to do a GET /address?street=xxx&nr=xxx&zipcode=xxx
(and a few more parameters) and return a 200 OK if correct or a 404 Not found if not correct, which may be more REST-ful?
I started doing option 1) but the more I'm thinking about it, option 2) with the GET feels better...
Ideas?
From a RESTful perspective, you're really returning a new resource, call it AddressValidation, that will contain your true or false value. So one approach would be to do a POST to /addressvalidation?street=xxx
etc. I'd be fine with returning the result as JSON or using status codes. I'm not sure 404 is appropriate though; you might want to look at this discussion of validation return status codes.
I have the same issue with the GET /address?street=xxx&nr=xxx&zipcode=xxx
approach as you propose it. To me, if it returns 404, that means that the address is literally not found (ie doesn't exist in the database), rather than that it's invalid (eg the zipcode is an invalid format; there can't be any such address). Again, see the linked discussion; it seems like 400 is a more appropriate response.
怎么样?
GET /addressValidity?street=xxx&nr=xxx&zipcode=xxx
=>
200 OK
Content-Type: text/plain
true
I feel doing POST and returning status codes (200 OK if correct or a 404 Not found if not correct) is more restful. Because you are not GETting something GET doesn't look appropriate. You are POSTing some information to server and it does some processing (validation) and returns some response.
链接地址: http://www.djcxy.com/p/45398.html上一篇: PHP形式ajax“成功与失败”的消息