gpt4 book ai didi

kotlin - 如何使用 Ktor 服务器向其他服务器发送 POST 请求?

转载 作者:行者123 更新时间:2023-12-03 08:33:43 43 4
gpt4 key购买 nike

我使用 IDEA 生成模板,并注意到 Application.module 中的 runBlocking 如下:

    runBlocking {
// Sample for making a HTTP Client request
val message = client.post<JsonSampleClass> {
url("http://127.0.0.1:8080/path/to/endpoint")
contentType(ContentType.Application.Json)
body = JsonSampleClass(hello = "world")
}
}

但是当我这样写向另一个服务器(例如获取天气的服务器)发送 Post 请求时,我得到:

java.io.IOException: Broken pipe

我不知道是不是我写的方式不对或者是写错了地方。

最佳答案

当然,日期类值得 JsonSampleClass,您需要像过度增长响应中那样更改此类或使用 HttpResponse。示例:

runBlocking {
// Sample for making a HTTP Client request
val message = client.post<HttpResponse> { // or your data class
url("url")
contentType(ContentType.Application.Json)
setBody(JsonSampleClass(hello = "world"))
}
}

https://ktor.io/docs/request.html#body

关于kotlin - 如何使用 Ktor 服务器向其他服务器发送 POST 请求?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/64551352/

43 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com