简体   繁体   English

在 Ratpack 中使用 `prefix {}` 方法而不是 `all { byMethod { } }` 会导致 405 Method Not Allowed - 有什么问题?

[英]Using `prefix {}` method instead of `all { byMethod { } }` in Ratpack causes 405 Method Not Allowed - what's wrong?

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.我刚刚开始阅读“Learn Ratpack”,在本书开头的一个例子中,作者使用“all”、“byMethod”、“get”和“post”来举例说明如何解析请求数据,他的工作方式,但我尝试使用“前缀”、“获取”和“发布”,但我无法得到相同的结果,它返回 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 405版

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.如果您想对同一相对路径使用多个不同的 HTTP 方法,您仍然需要使用byMethod {}方法创建此类处理程序。 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 the get handler 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 the post {} is the first handler in the chain.) (在您的情况下,POST 请求因405 Method Not Allowed而失败,因为get处理程序处理该请求并在请求中发现了不正确的 HTTP 方法。如果您希望看到 GET 请求失败而不是 POST one - 重新排序方法post {}是链中的第一个处理程序。)

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.这个byMethod {}方法允许为同一个相对路径注册多个处理程序,这些处理程序将根据请求的 HTTP 方法进行解析。 In case of using prefix {} method you may access byMethod {} method in the path {} helper method:如果使用prefix {}方法,您可以在path {}辅助方法中访问byMethod {}方法:

import static ratpack.groovy.Groovy.ratpack

ratpack {

    handlers {

        prefix("parsing-request-data") {
            path {
                byMethod {
                    post {
                        response.send("A response returned from POST /parsing-request-data\n ")
                    }

                    get {
                        response.send("A response returned from GET /parsing-request-data\n")
                    }
                }
            }

            get("test") {
                response.send("A response returned from GET /parsing-request-data/test\n")
            }
        }
    }
}

And a few curl commands to test it:以及一些 curl 命令来测试它:

$ curl -i -X GET http://localhost:5050/parsing-request-data    
HTTP/1.1 200 OK
content-type: text/plain;charset=UTF-8
content-length: 51

A response returned from GET /parsing-request-data

$ curl -i -X POST http://localhost:5050/parsing-request-data    
HTTP/1.1 200 OK
content-type: text/plain;charset=UTF-8
content-length: 53

A response returned from POST /parsing-request-data

$ curl -i -X GET http://localhost:5050/parsing-request-data/test
HTTP/1.1 200 OK
content-type: text/plain;charset=UTF-8
content-length: 56

A response returned from GET /parsing-request-data/test

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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