'Post request a raw json with Ktor client

I want to make post request with Ktor but I get Error 400.

the curl looks like this :

curl --location --request POST 'https://api.jdoodle.com/execute' \
--header 'Content-Type: application/json' \
--data-raw '{
   "script" : "console.log('\''hello'\'')",
   "language": "nodejs",
   "versionIndex": "1",
   "clientId": "....",
   "clientSecret": "...."
}'

Here is my code :

val client = HttpClient {
        install(JsonFeature) {
            val json = Json {
                isLenient = true
                ignoreUnknownKeys = true
            }
            serializer = KotlinxSerializer(json)
        }
    }

fun fetchData(requestBody: Request): Response {
        return client.post(BASE_URL){
            contentType(ContentType.Application.Json)
            body = requestBody
        }
    }

@Serializable
data class Request(....)

What am I doing wrong ?



Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source