I've just started reading "Learn Ratpack", in one of the examples from the very beginning of the book, the author uses 'all', 'byMethod', 'get' and 'post' to exemplify how to parse request data, the way that he does it work, but I've tried using 'prefix', 'get' and 'post' but I can't get the same result, it's returning 405-Method Not Allowed.
I tried to find something in the docs, but I couldn't see why the behavior with 'prefix'.
Example version
import static ratpack.groovy.Groovy.ratpack
import ratpack.form.Form
ratpack {
handlers {
all {
byMethod {
get {
//In the exemple he sends a html form
}
post {
//And here he parses it.
}
}
}
}
}
405 version
import static ratpack.groovy.Groovy.ratpack
import ratpack.form.Form
ratpack {
handlers {
prefix("parsing-request-data") {
get{
//From here all the same
That's it, what am I missing?
If you want to use multiple different HTTP methods for the same relative path, you still need to create such handlers using
byMethod {}method. Otherwise, the first handler in the chain that matches the relative path handles the request, and it may fail or succeed. (In your case POST request fails with 405 Method Not Allowed because thegethandler handles the request and it finds an incorrect HTTP method in the request. If you would like to see the GET request to fail instead of the POST one - reorder methods so thepost {}is the first handler in the chain.)This
byMethod {}method allows to register multiple handlers for the same relative path, and those handlers will be resolved based on the request's HTTP method. In case of usingprefix {}method you may accessbyMethod {}method in thepath {}helper method:And a few curl commands to test it: