如何在Ktor客户端中设置内容类型字符集(不是接受字符集)



这里的初学者,有没有其他方法可以添加内容类型字符集而不是header("Content-Type", "application/json; charset=UTF-8")

val client = HttpClient {
Charsets {
Charsets.UTF_8
} // This sets the accept header only, does not set the content type charset
}
client.put<HttpResponse> {
contentType(ContentType.Application.Json)
url(url)
body =body
}

然而,这适用于

client.put<HttpResponse> {
header("Content-Type", "application/json; charset=UTF-8")
contentType(ContentType.Application.Json)
url(url)
body = body
}

非常感谢。

要使用Ktor API指定头,您必须给它一个参数:

client.put<HttpResponse> {
contentType(ContentType.Application.Json.withParameter("charset", "utf-8"))
url(url)
body = body
}

相关内容

最新更新