Warning: file_get_contents(/data/phpspider/zhask/data//catemap/4/kotlin/3.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Kotlin 如何使用Ktor服务器向其他服务器发送POST请求?_Kotlin_Server_Backend_Ktor - Fatal编程技术网

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

Kotlin 如何使用Ktor服务器向其他服务器发送POST请求?,kotlin,server,backend,ktor,Kotlin,Server,Backend,Ktor,我使用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")

我使用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")
        }
   }
runBlocking{
//发出HTTP客户端请求的示例
val message=client.post{
网址(“http://127.0.0.1:8080/path/to/endpoint")
contentType(contentType.Application.Json)
body=JsonSampleClass(hello=“world”)
}
}
但是,当我这样写向另一台服务器发送Post请求(例如获取天气的服务器)时,我得到:

java.io.IOException:管道破裂


我不知道如果我以错误的方式或在错误的位置编写它。

当然,date类值得JsonSampleClass,您需要在overgrowth响应中更改该类,或者使用HttpResponse。 例如:

runBlocking{
//发出HTTP客户端请求的示例
val message=client.post{//或您的数据类
url(“url”)
contentType(contentType.Application.Json)
body=您的数据类
}
}

您能否分享更多信息,如您是如何创建
HttpClient
的?我可以分享一个工作示例,但我不确定您缺少哪一部分
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
    }
}