具有Defaultrequest 2.x+的KTOR参数
我们从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 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
您仍然可以通过访问
url
属性:You can still add parameters in the
DefaultRequest
plugin by accessing theurl
property: