Solution 1 :
You can send a multipart/form-data
request using an instance of the MultiPartFormDataContent
class for a body or the submitFormWithBinaryData
method. For more information please read the documentation.
Ktor 1.6.*
val client = HttpClient(OkHttp)
val response: String = client.post("https://httpbin.org/post") {
body = MultiPartFormDataContent(parts = formData {
append("page", 2)
append("countPerPage", 20)
})
}
println(response)
Ktor 2.*
val client = HttpClient(OkHttp)
val response = client.post("https://httpbin.org/post") {
setBody(MultiPartFormDataContent(parts = formData {
append("page", 2)
append("countPerPage", 20)
}))
}
println(response.bodyAsText())
Problem :
I want to call an API with the body and send two parameters to this request in the android ktor client. how can I do this?
I wrote this code but it’s not working:
override suspend fun getListOfAllCompanies(): ResponseModel<List<NetworkAllCompaniesModel>> =
KtorModule.provideKtor().post( HttpRoutes.GET_LIST_OF_ALL_COMPANIES) {
body = Body1(page = 2, countPerPage = 20)
}.body()
}
@Serializable
data class Body1(
val page: Int,
val countPerPage: Int,
)
This is my request:
Comments
Comment posted by Aymen Ben Salah
Do you try to use retrofit??
Comment posted by Saeed Noshadi
@AymenBenSalah No I’m using ktor client, I want to know how can i send a form data in the POST in ktor
Comment posted by Aleksei Tirman
So do you want to send a POST request with the
Comment posted by Saeed Noshadi
@AlekseiTirman Yes
Comment posted by Saeed Noshadi
I Used this code but it’s not working: override suspend fun getListOfAllCompanies(): ResponseModel> = KtorModule.provideKtor().post(HttpRoutes.GET_LIST_OF_ALL_COMPANIES) { body = MultiPartFormDataContent(formData { append(“page”,1) append(“countPerPage”,10) }) }.body()