Skip to content
This repository was archived by the owner on Aug 24, 2023. It is now read-only.

gServ Custom Action Request Matching

Lee Collins edited this page Apr 21, 2015 · 1 revision

#Custom Matching Custom matching allows user-defined criteria. Criteria may specify header values, including content type and accept. Actions may be defined with custom matchers. There are 3 builtin matchers:

onlyIfContentType( types... )
onlyIfAccepts( types... )
onlyIfHeader( header, values... )

Examples

onlyIfAccepts

GServ.Resource('/books') {
    get "/:id:Integer" , 
        onlyIfAccepts("text/json") { bookId ->
        def bk = service.getBook(bookId)
        writeJson [book: bk]
    }
}.start(8080);

####Notes If the request does not have an Accept header with 'text/json' a 404 status code may be returned to the client.

onlyIfContentType

GServ.Resource('/books') {
    put "/:id:Integer" , 
        onlyIfContentType("text/json") { Book book, id ->
        def bk = service.updateBook(id, book)
        writeJson [book: bk]
    }
}.start(8080);

####Notes If the request does not have an Content-Type header with 'text/json' a 404 status code may be returned to the client.

onlyIfHeader

GServ.Resource('/books') {
    get "/:id:Integer" , 
        onlyIfHeader('Accept-Encoding'."gzip") { bookId ->
        def bk = service.getBook()
        writeJson [book: bk]
    }
}.start(8080);

####Notes If the request does not have an Accept-Encoding header including 'gzip', a 404 status code may be returned to the client.