Rest – .Net WebAPI URI convention for advanced searching /filtering

apiasp.net-web-apirest

I am relatively new to REST and WebAPI by Microsoft. We are implementing a hub REST service that will house several types of object gets and sets. Being the lead on the project, I am being tasked with coming up with the proper Uri design we are going with. I was wondering what thoughts were on war is better. Yes I specifically phased that without using the word "standard".

Here are the options my team and I are currently entertaining:

Http://servername/API/REST/Ldap/AD/employees?username=jsmith 
Http://servername/API/REST/Ldap/AD/employee/UserName?searchTerm=jsmith (this seems RPC to me)
Http://servername/API/REST/Ldap/AD/employees/getusername?searchterm?jsmith

We are also creating a Soap version hence the rest in the Uri.

Thanks for the input

Best Answer

I recommend you take a look at Web API Design from Brian Mulloy. He has several recommendations when it comes to searching and filtering.

Simplify associations - sweep complexity under the ‘?’

Most APIs have intricacies beyond the base level of a resource. Complexities can include many states that can be updated, changed, queried, as well as the attributes associated with a resource. Make it simple for developers to use the base URL by putting optional states and attributes behind the HTTP question mark. Keep your API intuitive by simplifying the associations between resources, and sweeping parameters and other complexities under the rug of the HTTP question mark.

Tips for search

While a simple search could be modeled as a resourceful API (for example, dogs/?q=red), a more complex search across multiple resources requires a different design. If you want to do a global search across resources, we suggest you follow the Google model:

Global search

/search?q=fluffy+fur

Here, search is the verb; ?q represents the query.

Scoped search

To add scope to your search, you can prepend with the scope of the search. For example, search in dogs owned by resource ID 5678

/owners/5678/dogs?q=fluffy+fur

Notice that the explicit search has been dropped from the URL and instead it relies on the parameter 'q' to indicate the scoped query.

Pagination and partial response

Support partial response by adding optional fields in a comma delimited list.

/dogs?fields=name,color,location

Use limit and offset to make it easy for developers to paginate objects.

/dogs?limit=25&offset=50

Related Topic