【问题标题】:How can I send POST request to other server with Ktor server?如何使用 Ktor 服务器向其他服务器发送 POST 请求?
【发布时间】:2021-02-09 13:11:43
【问题描述】:

我使用 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: 损坏的管道

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

【问题讨论】:

  • 您能否分享更多信息,例如您是如何创建HttpClient 的?我可以分享一个工作示例,但我不确定您缺少哪一部分

标签: kotlin server backend ktor


【解决方案1】:

当然,日期类值得 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)
        body = your data class
    }
}

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2012-12-12
    • 1970-01-01
    • 2018-01-09
    • 1970-01-01
    • 2019-03-16
    • 1970-01-01
    相关资源
    最近更新 更多