Helio
05/20/2022, 1:24 AM1 - Converting an URL to URI usingThe snippet below works forthrows an Exceptionio.ktor.http.Url
java.net.URISyntaxException: Illegal character in query at index
Ktor 1.6.8
, but it throws the exception for Ktor 2.0.1
// The input parameters used for testing are:
// baseUrl = <http://bbc-pipelines-build-info.integration.test:8082>
// urlQueryParameters = build-info?workspace-id={5734a707-dc63-47a1-b63f-03e5d80efad5}
private fun buildBuildInfoUrl(baseUrl: String, urlQueryParameters: String) =
return Url("$baseUrl/$urlQueryParameters").toURI().toString()
The response is:
<http://bbc-pipelines-build-info.integration.test:8082/build-info?workspace-id=%7B5734a707-dc63-47a1-b63f-03e5d80efad5%7D>
Exception thrown by Ktor 2.0.1
java.net.URISyntaxException: Illegal character in query at index 78: <http://bbc-pipelines-build-info.integration.test:8082/build-info?workspace-id={5734a707-dc63-47a1-b63f-03e5d80efad5}>
at java.base/java.net.URI$Parser.fail(URI.java:2913)
at java.base/java.net.URI$Parser.checkChars(URI.java:3084)
at java.base/java.net.URI$Parser.parseHierarchical(URI.java:3172)
at java.base/java.net.URI$Parser.parse(URI.java:3114)
at java.base/java.net.URI.<init>(URI.java:600)
at io.ktor.http.URLUtilsJvmKt.toURI(URLUtilsJvm.kt:54)
2 - Even though I’m using theLooking through the snippet below in which I define my HttpClient I would assume thatplugin with the HttpClient from my integration tests, when I set the following attributesdefaultRequest
, during the request time thehost/port/protocol
is replaced from the value I gave withhost
.localhost
host
used for the request should be bbc-pipelines-build-info.integration.test
.
fun buildClient(): HttpClient {
val buildInfoUrl = URLBuilder("<http://bbc-pipelines-build-info.integration.test:8082>").build()
return HttpClient(MockEngine) {
install(ContentNegotiation) {
json(Json {
isLenient = true
ignoreUnknownKeys = true
useArrayPolymorphism = true
})
}
defaultRequest {
url {
protocol = buildInfoUrl.protocol
port = buildInfoUrl.port
host = buildInfoUrl.host
}
header(HttpHeaderConstants.X_SLAUTH_EGRESS, true)
}
bbcPipelinesUrlHandler(buildInfoUrl)
}
}
However, when I make the request with url {encodedPath = "build-info"}
the request is done using the host as being localhost
(see snippet below). When I replace the url { encodedPath = "build-info"}
with buildInfoHttpClient.submitForm("build-info",...)
instead of localhost
, the request is sent to bbc-pipelines-build-info.integration.test
host.
suspend fun getBitbucketPipelineBuildInfo(
workspaceId: String,
): BitbucketBuildInfoResponseModel = retrying {
buildInfoHttpClient.submitForm(url {encodedPath = "build-info"}, formParameters = Parameters.build {
append(BitbucketPipelineQueryParameters.WORKSPACE_ID.field, workspaceId)
}, encodeInQuery = true).body()
}
Do you reckon you could provide an assistance for these 2 issues, please?
Thanks heaps!Paul Woitaschek
05/20/2022, 4:12 AMHelio
05/20/2022, 5:37 AMhost
issue could be related with the bug.Aleksei Tirman [JB]
05/20/2022, 3:56 PMUrlBuilder
and then encoded it when returning a string representation. That led to a loss of original symbols in URL because there are multiple representations for some symbols. In Ktor 2.0.0 that behavior was fixed so we store an original URL string as is.Helio
05/21/2022, 3:20 AMAleksei Tirman [JB]
05/22/2022, 12:34 PMSo, from your Ktor expertise, there is no way in which I could only covert the curly braces from the URL using the UrlBuilder anymore?You can add query paramerters to an
UrlBuilder
via the parameters
property:
val uri = URLBuilder().apply {
host = "bbc-pipelines-build-info.integration.test"
port = 8082
encodedPath = "/build-info"
parameters.append("workspace-id", "{5734a707-dc63-47a1-b63f-03e5d80efad5}")
}.build().toURI()
I wonder if theYour code should work as expected in Ktor 2.0.2.issue could be related with the bughost
Helio
05/22/2022, 10:22 PMAleksei Tirman [JB]
06/16/2022, 6:07 AMHelio
06/16/2022, 6:09 AMAleksei Tirman [JB]
06/16/2022, 6:11 AMHelio
06/16/2022, 6:11 AM