Hey all, I am looking at this simple Ktor client c...
# ktor
j
Hey all, I am looking at this simple Ktor client call:
Copy code
...
        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?
Originally as following the docs as so also doesn't sent the body.
Copy code
val 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)
                    }
                }
            }
However, this style of builder works fine:
Copy code
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)
                    }
                }
a
The problem is that the
formData
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:
Copy code
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)
    }
}
kodee electrified 2
j
Cheers @Aleksei Tirman [JB]