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?
If not carefully considered, booleans can: Obstruct API Extensibility. Mask and obfuscate Domain Clarity. Hamper Code Readability and Maintainability.
The default output format of a WebAPI webservice is XML. To change this to JSON format, you have to manually remove the media type “application/xml” from the configuration of the supported media types.
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 (i.e. doesn't exist in the database), rather than that it's invalid (e.g. 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.
How about?
GET /addressValidity?street=xxx&nr=xxx&zipcode=xxx
=>
200 OK
Content-Type: text/plain
true
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With