【问题标题】:How to call an internal endpoint in Ktor?如何在 Ktor 中调用内部端点?
【发布时间】:2021-05-31 13:43:02
【问题描述】:

我试图弄清楚如何调用内部端点并在 Ktor 中获得响应。我看到了有关如何在内部重定向的帖子,但它们没有返回响应。

例如。我有一个返回 foo 的获取请求。我有另一个返回 bar 的 get 请求。我有一个应该返回 foobar 的 getrequest。我可以自己调用这些端点,而不是重写 foo 和 bar 中的逻辑。

get("/foo") {
    call.respondText("foo") // except without relying on client to handle HTTP
}
get("/bar") {
    call.respondText("bar")
}
get("/foobar") {
    call.respondText(get("/foo") + get("/bar")) // returns foobar
}

【问题讨论】:

    标签: kotlin ktor


    【解决方案1】:

    您在这里要做的是将功能抽象为函数,然后调用这些函数。但是,我会根据每条路由单独处理请求和响应。

    get("/foo") {
        val result = functionHandlingFoo()
        // send out result here
    }
    get("/bar") {
        val result = functionHandlingBar()
        // send out result here
    }
    get("/foobar") {
        val resultFoo = functionHandlingFoo()
        val resultBar = functionHandlingBar()
        // process result and send out
    }
    

    【讨论】:

      猜你喜欢
      • 2020-06-12
      • 1970-01-01
      • 2020-05-22
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2019-09-22
      • 2021-08-02
      相关资源
      最近更新 更多