Hi does anyone use Ktorfit? I'm trying to send an ...
# multiplatform
i
Hi does anyone use Ktorfit? I'm trying to send an object as Body:
Copy code
@POST("auth/signup-user")
    suspend fun register(
        @Body registerUser: RegisterUser,
    ): Response<RemoteUser>
But I'm getting this error making the request:
The body type is: class com.example.shared.data.remote.models.LoginUser, with Content-Type: null.
My httpClient:
Copy code
Ktorfit.Builder().httpClient(Engine) {
        configurePlatform()

        expectSuccess = true

        install(HttpTimeout) {
            connectTimeoutMillis = 30.seconds.inWholeMilliseconds
            requestTimeoutMillis = 30.seconds.inWholeMilliseconds
            socketTimeoutMillis = 2.minutes.inWholeMilliseconds
        }
        install(HttpRequestRetry) {
            retryOnServerErrors(maxRetries = 5)
            retryOnExceptionIf(maxRetries = 5) { _, cause ->
                cause is TimeoutCancellationException ||
                    cause is CancellationException ||
                    cause is kotlinx.coroutines.CancellationException
            }
            exponentialDelay()
        }
        install(ContentNegotiation) {
            json(
                Json {
                    isLenient = false
                    prettyPrint = true // Useful for debugging
                    ignoreUnknownKeys = true
                    classDiscriminator = "type"
                    // serializersModule =
                },
             contentType = ContentType.Application.Json
            )
        }
        
        install(Logging) {
            level = LogLevel.ALL
            logger =
                object : Logger {
                    override fun log(message: String) {
                        co.touchlab.kermit.Logger.i("HttpClient") { message }
                    }
                }
        }
    }
Where should I tell ktorfit(or ktor) that the Body has to be sent as Json? As usual Thanks in advance
I solved it adding
Copy code
defaultRequest {
            header("Content-Type", "application/json; charset=utf-8")
        }
To the httpClient, but I don't know if that's the best way, because I'm not always sending Json, only in the @ POST's
i
You can do it that way or add headers on the methods directly like this
@Headers("Content-Type: application/json")
i
That's what I wanted, idk why I tried before but @Headers("Accept: app.../json") of course that was wrong but I didn't notice, thanks!
104 Views