Need feedbck on the quality of REST URL

Viewed 41

For getting the latest valid address (of the logged in user), how RESTful is the following URL?

GET /addresses/valid/latest

Probably

GET /addresses?valid=true&limit=1

is the best, but it should then return a list. And, I'd like to return an object rather then a list.

Any other suggestions?

2 Answers

Your url structure doesn't have much to do with how RESTful something is.

So lets assume which one is the 'best'. Also a bit hard to say, pretty subjective.

I would generally avoid a pattern like /addresses/valid/latest. This kinda suggest that there is a 'latest resource' in the 'valid collection', in the 'addresses collection'.

So I like your other suggestion a bit better, because it suggests that you're using an 'addresses' collection, filtering by valid items and only showing 1.

If you don't want all kinds of parameters, I would be more inclined to find a url pattern that's not literally 'addresses, but only the valid, but only the latest', but think about what the purpose is of the endpoint. Maybe something that's easier to remember like /fresh-address =)

how RESTful is the following URL?

Any identifier that satisfies the production rules described by RFC 3986 is RESTful.

General purpose components are not supposed to derive semantics from identifiers, they are opaque. Which means that the server is free to encode information into those identifiers at its own discretion.

Consider Google search: does your browser care what URI is used as the target of the search form? Does your browser care about the href provided by Google with each search result? In both cases, the browser just does what it is told, which is to say it creates an HTTP request based on the representation of application state that was provided by the server.

URI are in the same broad category as variable names in a programming language - the machines don't care so long as the spellings are consistent with some simple constraints. People care, so there are some benefits to having a locally consistent and logical scheme.

But there are contexts in which easily guessed URI are not what you want. See Mark Seemann 2013.

Since the semantic content of the URI is reserved for use by the server only, it follows that the server can choose to encode that information into path segments or the query part. Or both.

Spellings that can be described by a URI Template can be very powerful. The most familiar URI template is probably an HTML form using the GET method, which encodes key value pairs onto the query part of the URI; so you should think about whether that's a use case you want to support.

Related