简体   繁体   中英

REST url param naming convention

I have a REST call to search students

/students?name=&no=

name and no are optional.

I am searching students with name contains. ie if I send name="sh" then it will return all students which name contains "sh".

AND

for no, it is no starts with ie if no=10 then it will return all students with no starts with 10

What are standard naming convention for such parameters. Can I say

/students?nameContains=&noStartsWith=

OR

/students?name=&no=

Which one is correct way ?

There exist no standard naming conventions for query parameters. In your specific case, it depends on the likelihood of future requirements. If it's just nameContains , well, that's much better than name . But if you're going to need nameContains , nameStartsWith , nameEndsWith , etc., that's going to get messy fast. Then you should consider either a separate resource that specifies search criteria or adding wildcards to your name , such as:

?name=Bob*  <!-- name starts with Bob -->
?name=*h   <!-- name ends with h -->
?name=Sa?  <!-- name has three letters and starts 'Sa' --> 

For truly complex cases, you need to think about either supporting regexes or using an existing library to handle search. Also think about if you need to support ?nameIgnoreCase , which would argue strongly in favor of wildcards rather than multiple query parameters.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM