简体   繁体   English

我可以使用groovy-dsl允许从所有来源删除吗?

[英]Can I allow DELETE from all origins with groovy-dsl?

Building a quick and dirty REST API with Ratpack script; 使用Ratpack脚本构建快速而肮脏的REST API; can't figure out how to allow DELETE from all origins. 无法弄清楚如何允许从所有来源删除。

I've tried setting headers within delete , and using all (as in sample code.) Sending DELETE with curl, postman, everything always returns 405. Am I missing something simple? 我尝试在delete设置标头,并使用all标头(如示例代码中所示)。使用curl和postman发送DELETE时,一切总是返回405。我是否缺少一些简单的东西?

@Grapes([
  @Grab('io.ratpack:ratpack-groovy:1.6.1')
])

ratpack {
    handlers {
        all {
            MutableHeaders headers = response.headers
            headers.set("Access-Control-Allow-Origin", "*")
            headers.set("Access-Control-Allow-Methods", "GET, PUT, POST, DELETE")
            next()
        }

        post("product") {
            ...
        }

        get("product/:id") {
            ...
        }

        delete("product/:productId") {
            // always returns 405
            ...
        }
    }
}

You see HTTP/1.1 405 Method Not Allowed response status because your request gets handled by the get("product/:id") handler. 您会看到HTTP/1.1 405 Method Not Allowed响应状态,因为您的请求由get("product/:id")处理程序处理。 If you want to use the same path for multiple HTTP methods, you can use prefix combined with byMethod method to define multiple handlers for the same path. 如果要对多个HTTP方法使用同一路径,则可以将prefixbyMethod方法结合使用, byMethod同一路径定义多个处理程序。

Consider the following example: 考虑以下示例:

import ratpack.http.MutableHeaders

import static ratpack.groovy.Groovy.ratpack

ratpack {
    handlers {
        all {
            MutableHeaders headers = response.headers
            headers.set("Access-Control-Allow-Origin", "*")
            headers.set("Access-Control-Allow-Methods", "GET, PUT, POST, DELETE")
            next()
        }

        prefix("product") {
            post {
                render("POST /product")
            }

            prefix(":id") {
                path {
                    byMethod {
                        get {
                            render("GET /product/${allPathTokens.id}")
                        }

                        delete {
                            render("DELETE /product/${allPathTokens.id}")
                        }
                    }
                }
            }
        }
    }
}

As you can see in the example above, you can nest prefixes. 如上例所示,您可以嵌套前缀。 We can test it with the following curl requests: 我们可以使用以下curl请求对其进行测试:

$ curl -X POST http://localhost:5050/product
POST /product%                                                                                                                                                                                                                                 

$ curl -X GET http://localhost:5050/product/test
GET /product/test%                                                                                                                                                                                                                             

$ curl -X DELETE http://localhost:5050/product/test
DELETE /product/test%  

If you are interested in more details, I have written a blog post some time ago with a similar example - https://e.printstacktrace.blog/using-the-same-prefix-with-different-http-methods-in-ratpack/ 如果您对更多细节感兴趣,我前段时间写了一个博客文章,并提供了类似的示例-https://e.printstacktrace.blog/using-the-same-prefix-with-different-http-methods-in-腰包/

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

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