具有Defaultrequest 2.x+的KTOR参数

发布于 2025-01-24 17:14:30 字数 799 浏览 1 评论 0原文

我们从KTOR 1.x迁移到2.x+,并注意到我们无法再添加默认参数:

defaultRequest {
        if (internal) {
            header(WebserviceConstants.KEY_TOKEN, getAccessToken())

            parameter(WebserviceConstants.KEY_LANG, localeHelper.getLanguage())
            parameter(WebserviceConstants.KEY_STORE, getStore(localeHelper))
        }
        host = hostAddress
        url {
            protocol = URLProtocol.HTTPS
        }
    }

我们仍然可以在每个调用中添加参数:

val response: HttpResponse = client.get("http://localhost:8080/products") {
    parameter(WebserviceConstants.KEY_LANG, localeHelper.getLanguage())
    parameter(WebserviceConstants.KEY_STORE, getStore(localeHelper))
}

但是,如果我们每个调用都需要某些参数,那很烦人。我们尝试将它们添加为属性,并通过“迁移指南”以及我们能找到的任何文档脱颖而出。

还有另一种方法吗?

We migrated from KTOR 1.X to 2.X+ and noticed that we can't add default parameters any more like this:

defaultRequest {
        if (internal) {
            header(WebserviceConstants.KEY_TOKEN, getAccessToken())

            parameter(WebserviceConstants.KEY_LANG, localeHelper.getLanguage())
            parameter(WebserviceConstants.KEY_STORE, getStore(localeHelper))
        }
        host = hostAddress
        url {
            protocol = URLProtocol.HTTPS
        }
    }

We can still add parameters with every call:

val response: HttpResponse = client.get("http://localhost:8080/products") {
    parameter(WebserviceConstants.KEY_LANG, localeHelper.getLanguage())
    parameter(WebserviceConstants.KEY_STORE, getStore(localeHelper))
}

But that's annoying if we need certain parameters with every call. We tried adding them as attributes and skimmed through the migration guide as well as any documentation we could find.

Is there another way?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

新雨望断虹 2025-01-31 17:14:30

您仍然可以通过访问url属性:

url.parameters.append(WebserviceConstants.KEY_LANG, localeHelper.getLanguage().toString())
url.parameters.append(WebserviceConstants.KEY_STORE, getStore(localeHelper).toString())

You can still add parameters in the DefaultRequest plugin by accessing the url property:

url.parameters.append(WebserviceConstants.KEY_LANG, localeHelper.getLanguage().toString())
url.parameters.append(WebserviceConstants.KEY_STORE, getStore(localeHelper).toString())
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文