John Lamb
05/16/2024, 6:19 AM...
runBlocking {
val response: HttpResponse = client.use { cl ->
cl.submitForm {
url {
protocol = URLProtocol.HTTP
host = url
path(tenant, path)
}
headers {
append(HttpHeaders.Accept, ContentType.Application.Json)
append(HttpHeaders.AcceptCharset, Charsets.UTF_8.name())
append(HttpHeaders.ContentType, ContentType.Application.FormUrlEncoded)
}
formData {
append("grant_type", "client_credentials")
append("scope", scope)
append("client_id", clientId)
append("client_secret", clientSecret)
}
}
}
...
This call keeps 400' so I dropped it to HTTP and can see in the network traffic it is not sending the form body. Am I applying the formData incorrectly here?John Lamb
05/16/2024, 6:29 AMval response: HttpResponse = client.use { cl ->
cl.submitForm {
url {
protocol = URLProtocol.HTTPS
host = url
path(tenant, path)
}
headers {
append(HttpHeaders.Accept, ContentType.Application.Json)
append(HttpHeaders.AcceptCharset, Charsets.UTF_8.name())
append(HttpHeaders.ContentType, ContentType.Application.FormUrlEncoded)
}
parameters {
append("grant_type", "client_credentials")
append("scope", scope)
append("client_id", clientId)
append("client_secret", clientSecret)
}
}
}
John Lamb
05/16/2024, 6:56 AMcl.submitForm {
url {
protocol = URLProtocol.HTTPS
host = url
path(tenant, path)
}
headers {
append(HttpHeaders.Accept, ContentType.Application.Json)
append(HttpHeaders.AcceptCharset, Charsets.UTF_8.name())
append(HttpHeaders.ContentType, ContentType.Application.FormUrlEncoded)
}
parameters {
append("grant_type", "client_credentials")
append("scope", scope)
append("client_id", clientId)
append("client_secret", clientSecret)
}
}
Aleksei Tirman [JB]
05/16/2024, 7:22 AMformData
method is a "pure" function so it doesn't affect the request. Also, this method is for sending the request body of the multipart/form-data
type. If you need to send the body of the application/x-www-form-urlencoded
type, you can use the Parameters.build
method:
cl.submitForm(formParameters = Parameters.build {
append("grant_type", "client_credentials")
append("scope", scope)
append("client_id", clientId)
append("client_secret", clientSecret)
}) {
url {
protocol = URLProtocol.HTTP
host = url
path(tenant, path)
}
headers {
append(HttpHeaders.Accept, ContentType.Application.Json)
append(HttpHeaders.AcceptCharset, Charsets.UTF_8.name())
append(HttpHeaders.ContentType, ContentType.Application.FormUrlEncoded)
}
}
John Lamb
05/16/2024, 7:48 AM