question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

ktor client does not log request body

See original GitHub issue

Ktor Version and Engine Used (client or server and name) ver: 1.3.1, 1.3.2 engine: Apache app: client dependencies:

dependencies {
    implementation(kotlin("stdlib-jdk8"))
    implementation(platform("io.ktor:ktor-bom:1.3.2"))
    implementation("io.ktor:ktor-client-core")
    implementation("io.ktor:ktor-client-logging-jvm")
    implementation("io.ktor:ktor-client-apache")
    implementation("io.ktor:ktor-client-json-jvm")
    implementation("org.slf4j:slf4j-simple:1.7.26")
    implementation("org.apache.httpcomponents:httpasyncclient:4.1.4")
}

Describe the bug Request body is not logged even with LogLevel.ALL

To Reproduce

package com.example

import io.ktor.client.HttpClient
import io.ktor.client.engine.apache.Apache
import io.ktor.client.features.logging.DEFAULT
import io.ktor.client.features.logging.LogLevel
import io.ktor.client.features.logging.Logger
import io.ktor.client.features.logging.Logging
import io.ktor.client.request.post
import io.ktor.util.KtorExperimentalAPI
import kotlinx.coroutines.runBlocking

@KtorExperimentalAPI
fun main() {
    runBlocking {
        val client = HttpClient(Apache) {
            install(Logging) {
                logger = Logger.DEFAULT
                level = LogLevel.ALL
            }
        }
        client.post<String>("http://localhost:8080") {
            body = """{ "test": "testVal" }"""
        }
    }
}

Expected behavior Request body is in the log

Actual result

[main] INFO io.ktor.client.HttpClient - REQUEST: http://localhost:8080/
[main] INFO io.ktor.client.HttpClient - METHOD: HttpMethod(value=POST)
[main] INFO io.ktor.client.HttpClient - COMMON HEADERS
[main] INFO io.ktor.client.HttpClient - -> Accept-Charset: UTF-8
[main] INFO io.ktor.client.HttpClient - -> Accept: */*
[main] INFO io.ktor.client.HttpClient - CONTENT HEADERS
[main] INFO io.ktor.client.HttpClient - BODY Content-Type: text/plain; charset=UTF-8
[main] INFO io.ktor.client.HttpClient - RESPONSE: 200 
---   remaining response stuff skipped for brevity  ---

Issue Analytics

  • State:closed
  • Created 4 years ago
  • Reactions:17
  • Comments:5 (1 by maintainers)

github_iconTop GitHub Comments

1reaction
huehnerladycommented, Jul 22, 2020

Any update on this? We run into the same issue here and seeing the request body would help a lot

0reactions
e5lcommented, Apr 5, 2021

Fixed in 1.4.1

Read more comments on GitHub >

github_iconTop Results From Across the Web

How to log requests in ktor http client? - kotlin - Stack Overflow
You can achieve this with the Logging feature. First add the dependency: implementation "io.ktor:ktor-client-logging-native:$ktor_version".
Read more >
Logging - Ktor
Ktor client provides the capability to log HTTP calls using the Logging plugin. This plugin provides different logger types for different ...
Read more >
Call logging - Ktor
You can learn about general logging configuration from the Logging topic. The CallLogging plugin allows you to log incoming client requests.
Read more >
Making requests | Ktor
Set the body of a request, for example, a plain text, a data object, or form parameters. These parameters are exposed by the...
Read more >
Logging - Ktor
You can also install and configure the CallLogging plugin to log client requests. For the Native server, Ktor provides a logger that prints ......
Read more >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found