Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Returning true/false in REST service?

Tags:

rest

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?

like image 894
Johan Danforth Avatar asked Apr 28 '11 14:04

Johan Danforth


People also ask

Why you shouldn't use booleans in rest APIS?

If not carefully considered, booleans can: Obstruct API Extensibility. Mask and obfuscate Domain Clarity. Hamper Code Readability and Maintainability.

Can output format be changed in rest?

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.


Video Answer


2 Answers

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.

like image 121
Jacob Mattison Avatar answered Oct 03 '22 05:10

Jacob Mattison


How about?

GET /addressValidity?street=xxx&nr=xxx&zipcode=xxx
=> 
200 OK
Content-Type: text/plain

true
like image 31
Darrel Miller Avatar answered Oct 02 '22 05:10

Darrel Miller