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

How can I send POST request to other server with Ktor server?

我使用 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

不知道是我写错了还是写错了地方

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

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