Computer scienceBackendKtorKtor Client

Ktor Client: First view

POST request without headers to Fake API

Report a typo

In theory, we looked at how to make a POST request to the Fake API to create a todo (https://jsonplaceholder.typicode.com/todos):

import io.ktor.client.*
import io.ktor.client.engine.cio.*
import io.ktor.client.request.*
import io.ktor.client.statement.*
import io.ktor.http.*

suspend fun main(args: Array<String>) {
    val client = HttpClient(CIO)
    val response: HttpResponse = client.post("https://jsonplaceholder.typicode.com/todos") {
        headers {
            append(HttpHeaders.ContentType, "application/json")
        }
        setBody("""{"userId": 1, "title": "My new todo!"}""")
    }

    println(response.status)
    println(response.headers)
    println(response.bodyAsText())

    client.close()
}

Modify the code from theory so that the Content-Type header is not sent to the server.

Run this code yourself and see what response the server returns. Does it differ from what it was in theory?

Copy the response body and paste it into the response field.

Enter a short text

Create a free account to access the full topic