Home > Software design >  Ktor parameters with DefaultRequest 2.X
Ktor parameters with DefaultRequest 2.X

Time:04-29

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?

CodePudding user response:

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())
  • Related