Skip to content

Snappy1

  • Home
  • Android
  • What
  • How
  • Is
  • Can
  • Does
  • Do
  • Why
  • Are
  • Who
  • Toggle search form

[FIXED] kotlin – How can I call post request with body in android tor client

Posted on November 11, 2022 By

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:

enter image description here

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()

READ  [FIXED] android - Flutter shared_preferences Build failed with an exception
Powered by Inline Related Posts
Android Tags:android, kotlin, ktor, ktor-client

Post navigation

Previous Post: [FIXED] android developer api – there is a ‘Late Initialization Error’ during flutter development
Next Post: [FIXED] android – How to set SSL pinning using retrofit for Firebase Realtime Database Url?

Related Posts

[FIXED] Write a log to a txt file android Android
[FIXED] kotlin – android ViewModelProverders.of does not seem Android
[FIXED] android – How can i create this Curved Bottom ImageView? Android
[FIXED] Apollo Java GraphQL Client: generateApolloSources failing Android
[FIXED] Query Firestore in Firebase Android Android
[FIXED] java – When I run my application on my Android Phone from Android Studio, it shows blank screen Android

Archives

  • March 2023
  • February 2023
  • January 2023
  • December 2022
  • November 2022
  • October 2022
  • September 2022

Categories

  • ¿Cómo
  • ¿Cuál
  • ¿Cuándo
  • ¿Cuántas
  • ¿Cuánto
  • ¿Qué
  • Android
  • Are
  • At
  • C'est
  • Can
  • Comment
  • Did
  • Do
  • Does
  • Est-ce
  • Est-il
  • For
  • Has
  • Hat
  • How
  • In
  • Is
  • Ist
  • Kann
  • Où
  • Pourquoi
  • Quand
  • Quel
  • Quelle
  • Quelles
  • Quels
  • Qui
  • Should
  • Sind
  • Sollte
  • Uncategorized
  • Wann
  • Warum
  • Was
  • Welche
  • Welchen
  • Welcher
  • Welches
  • Were
  • What
  • What's
  • When
  • Where
  • Which
  • Who
  • Who's
  • Why
  • Wie
  • Will
  • Wird
  • Wo
  • Woher
  • you can create a selvedge edge: You can make the edges of garter stitch more smooth by slipping the first stitch of every row.2022-02-04
  • you really only need to know two patterns: garter stitch

Recent Posts

  • What are the main features of Islamic education?
  • Is the Jeep 4xe worth it?
  • How does the ringer work on cast iron?
  • What is the biggest size interior door?
  • Is blue raspberry an original Jolly Rancher flavor?

Recent Comments

No comments to show.

Copyright © 2023 Snappy1.

Powered by PressBook Grid Dark theme