11

我不知道如何application/x-www-form-urlencoded POST在 Ktor 中发送请求。我submitForm在 Ktor 的文档中看到了一些助手,但他们没有按预期发送请求。

我想要的是复制这种卷曲线行为:

curl -d "param1=lorem&param2=ipsum" \
     -H "Content-Type: application/x-www-form-urlencoded; charset=UTF-8" \
     https://webservice/endpoint

我的依赖是io.ktor:ktor-client-cio:1.0.0.

4

2 回答 2

25

经过几次尝试,我设法使用以下代码发送请求:

val url = "https://webservice/endpoint"
val client = HttpClient()
return client.post(url) {
    body = FormDataContent(Parameters.build {
        append("param1", "lorem")
        append("param2", "ipsum")
    })
}
于 2018-12-03T14:10:15.657 回答
1
val response: HttpResponse = client.submitForm(
    url = "http://localhost:8080/get",
    formParameters = Parameters.build {
        append("first_name", "Jet")
        append("last_name", "Brains")
    },
    encodeInQuery = true
)



https://ktor.io/docs/request.html#form_parameters
于 2021-09-29T12:13:04.767 回答