Suresh Maidaragi
11/21/2023, 10:34 AMnull
values from HTTP post bodyBerkay Özkan
11/21/2023, 10:37 AMinstall(ContentNegotiation) {
json(
Json {
ignoreUnknownKeys = true
},
)
}
Suresh Maidaragi
11/21/2023, 10:45 AMimport kotlinx.serialization.SerialName
import kotlinx.serialization.Serializable
@Serializable
data class LoginRequestBody(
@SerialName("name")
val name: String,
@SerialName("last_name")
val lastName: String?=null,
@SerialName("password")
val password: String?=null
)
suspend fun postLogin(loginBody: LoginRequestBody): HttpResponse {
return httpClient.post("url") {
contentType(ContentType.Application.Json)
setBody(loginBody)
}
}
In this above code I don't want to post the key lastName
to http post request, because it can be null
.
Whenever lastName
is null I want to ignore in post requestbody
Suresh Maidaragi
11/21/2023, 11:19 AMBerkay Özkan
11/21/2023, 12:20 PMval r = LoginRequestBody("Test")
val x = Json.encodeToString(r)
println(x)
default behavior will be
{
"name": "Test"
}
Suresh Maidaragi
11/21/2023, 12:27 PM