So for instance, say i have an API on a webapp, and i wish to use the same controllers and actions in the API as the rest of the webapp.
In my urlmappings file i have
"/api/$version/$apiKey/$controller/$acion/$id?"
and i also have a mapping like this:
"/blog/$year/$month/$day/$action" {
controller = 'blog'
}
Now the question is, can i somehow prefix the api urlmapping to the blog urlmapping so i can benefit from the $year, $month, $day variables? in such a way that a GET request to the following url would be valid:
GET /api/0.1/bs23mk4m2n4k/blog/2001/01/05/list
or am i forced to do the following request instead?
GET /api/0.1/bs23mk4m2n4k/blog/list?year=2004&month=01&day=05
Need help from an urlmappings GURU or a groovy runtime urlmappings maniuplation WIZARD :)
I want a solution that can reuse existing non-api urmappings, instead of having to redeclare them with the api path as a prefix.
You could have an ApiController strip off the api parameters, then redirect to the blog controller. For example:
"/api/$version/$apiKey/$rest**" {
controller:'api'
action:'default'
}
import org.codehaus.groovy.grails.web.util.WebUtils
class ApiController {
def grailsUrlMappingsHolder
def default = {
// validate apiKey, etc
WebUtils.forwardRequestForUrlMappingInfo(request, response, grailsUrlMappingsHolder.match("/${params.rest}"))
}
}
The API controller has access to the version and apiKey params, and passes on the rest of the params to be processed by the blog controller's UrlMapping.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With