-
Notifications
You must be signed in to change notification settings - Fork 9
gServ Request URL Action Matching
#Basic URL Matching By Example
###Given
new GServ().http {
get "/books/inventory", { ->
write "text/plain", "Here is the inventory."
}
}.start(8080);
###Match Examples
http://yourhost:8080/books/inventory
###Given
new GServ().http {
get "/books/:id", { bookId ->
write "text/plain", "Getting book $bookId."
}
}.start(8080);
###Match Examples
http://yourhost:8080/books/24
http://yourhost:8080/books/45.45.34.55
http://yourhost:8080/books/anything
###Given - 1
new GServ().http {
get "/books/:id:Number", {Double bookId ->
write "text/plain", "BookId is a Number."
}
}.start(8080);
###Notes The value 'bookId' will be of type Double.
###Match Examples
http://yourhost:8080/books/24
http://yourhost:8080/books/45.45
http://yourhost:8080/books/76667645.343233546
###Given - 2
new GServ().http {
get "/books/:id:Integer", {Integer bookId ->
write "text/plain", "BookId is an Integer."
}
}.start(8080);
###Notes The value 'bookId' will be of type Integer.
###Match Examples
http://yourhost:8080/books/24
http://yourhost:8080/books/45
http://yourhost:8080/books/766676
###Given
new GServ().http {
get "/bySSN/:ssn:`\\d\\d\\d\\-\\d\\d\\-\\d\\d\\d\\d`" , { ssn ->
write "text/plain", "SSN is $ssn."
}
}.start(8080);
###Notes The regular expression in surounded in backtick characters (`). The backslashes need to be doubled so the compiler will know you mean a literal back slash and not a control character.
###Match Examples
http://yourhost:8080/byssn/123-54-4343
http://yourhost:8080/byssn/000-00-0000
###Given
new GServ().http {
get "/books/all?region=UK" , { ->
write "text/plain", "Books with Uk Region."
}
}.start(8080);
###Notes The query paramewters are matched verbatim. Any request without query value (region=UK) will NOT match.
###Match Examples
http://yourhost:8080/books/all?region=UK
http://yourhost:8080/books/all?region=US -- FAILS
###Given
new GServ().http {
get "/books/all?region=:region" , { region ->
write "text/plain", "Books with $region Region."
}
}.start(8080);
###Notes Any request without query parameter 'region' will NOT match. The parameter value is passed to the closure.
###Match Examples
http://yourhost:8080/books/all?region=UK
http://yourhost:8080/books/all?region=XX
http://yourhost:8080/books/all?region=ANY_THING
###Given
new GServ().http {
get "/books/:id:Integer?region=:region" , { bookId, region ->
write "text/plain", "Book $bookId in region $region."
}
}.start(8080);
###Notes Variable values are passed in-order to the handler closure.
###Match Examples
http://yourhost:8080/book/25?region=90
http://yourhost:8080/book/77777?region=XX
http://yourhost:8080/book/10101010?region=ANY_THING
http://yourhost:8080/book/0?region=ANY_THING
gServ © 2014-2017 Lee Collins. All rights reserved.