Home > Enterprise >  How can I see the json generated from Ktor?
How can I see the json generated from Ktor?

Time:07-14

I want to see the json result of the body but I have no idea how, I am recieving a 400 bad request and I am pretty sure the issue comes from "listOf(PostInvoiceResultInsideLineItems("API", 1, furiousInvoice.amount_inc_tax, "1", "FR_200"))"

In the API, it needs something like this:

"line_items": [
               {
                    "label": "API",
                    "quantity": 1,
                    "currency_amount": 2000,
                    "unit": "1",
                    "vat_rate": "FR_200"
               }
          ],

And the only way I could think of including the [] in the json was with listOf but I am not even sure it is writing it correctly hence the need to see exactly what Ktor is sending in Json.

suspend fun postInvoiceImport(furiousInvoice: GetInvoiceData, pennylaneId: String, vat: String, pdf_base64: String): Result<PostInvoiceResultResponse>{
    return client.post("${apiName}/import") {
        contentType(ContentType.Application.Json)
        setBody(
            PostInvoiceResult(
                PostInvoiceResultInside(
                    PostInvoiceResultInsideCustomer(pennylaneId),
                    listOf(PostInvoiceResultInsideLineItems("API", 1, furiousInvoice.amount_inc_tax, "1", "FR_200")),
                    furiousInvoice.invoice_date,
                    furiousInvoice.due_date,
                    "F"   furiousInvoice.id
                ),
                "false",
                pdf_base64
            )
        )
    }.toResource()

I tried to use Postman and even made all the necessary steps to capture HTTPS request but it doesn't detect Ktor requests.

CodePudding user response:

You can install the Logging plugin with the LogLevel.BODY level to observe a serialized request body. Here is an example:

val client = HttpClient(Apache) {
    install(ContentNegotiation) {
        json()
    }
    install(Logging) {
        level = LogLevel.BODY
    }
}

client.post("https://httpbin.org/post") {
    contentType(ContentType.Application.Json)
    setBody(listOf(123, 434, 777))
}

Here is an example log:

2022-07-13 09:01:19.127 [main]  INFO  io.ktor.client.HttpClient {} REQUEST: https://httpbin.org/post
METHOD: HttpMethod(value=POST)
BODY Content-Type: application/json
BODY START
[123,434,777]
BODY END
  • Related