How to do a post request using FORM-DATA or x-www-form-urlencoded with Android?

Adrian Euan

I have an issue with my code, I want to send a post with x-www-form-urlencoded or form-data, I need to send two parameters:

  • Email: String
  • Password: String

And after I receive two parameters in Json:

  • Token: String
  • Success: Boolean

It's important that the Email and Password are sent in Form-Data.

Postman example:

Postman example

Yahya M

Follow these steps to send Form data or URL encoded data in android using Retrofit.

Build.gradle

//Retrofit
implementation "com.squareup.okhttp3:okhttp:3.8.0"
implementation "com.squareup.okhttp3:logging-interceptor:3.8.0"
implementation ("com.squareup.retrofit2:retrofit:2.5.0"){
// exclude Retrofit’s OkHttp peer-dependency module and define your own module 
import
exclude module: 'okhttp'
}

Make interface class name

ApiInterface.kt

import retrofit2.Call
import retrofit2.http.*
interface ApiInterface {

@FormUrlEncoded
@POST("")
fun register(
    @Field("email") email: String,
    @Field("password") password: String?,
): Call<EmailResponse>
}

MainActivity.kt

val retrofit = Retrofit.Builder()
    .baseUrl(Constants.BASE_URL)
    .addConverterFactory(GsonConverterFactory.create())
    .build()
val service = retrofit.create(ApiInterface::class.java)
val call = service.register(email, password)


call!!.enqueue(object : Callback<EmailResponse> {
    override fun onResponse(
        call: Call<EmailResponse>,
        response: Response<EmailResponse>,
    ) {
        if (response.isSuccessful) {

            val emailResponse: EmailResponse = response.body()!!
            val token = emailResponse.token
            val success = emailResponse.success

        }
    }

    override fun onFailure(call: Call<EmailResponse>, t: Throwable) {
        Log.e(TAG, "onFailure: ")
    }
})

EmailResponse.kt

data class EmailResponse(
val token: String,
val success: Boolean,
)

And you Constants.BASE_URL will be https://URLEJEMPLO.com

Collected from the Internet

Please contact [email protected] to delete if infringement.

edited at
0

Comments

0 comments
Login to comment

Related

Spring - wrong encoding POST request with x-www-form-urlencoded

How to send post request with x-www-form-urlencoded body

How to force Angular2 to POST using x-www-form-urlencoded

Post a x-www-form-urlencoded request from React Native

CORS issue when trying to do JQuery post with json data, but not with plain text or x-www-form-urlencoded

RestSharp post request - Body with x-www-form-urlencoded values

Jersey client Post Request with x-www-form-urlencoded Fails

Send sensitive data using POST+application/x-www-form-urlencoded

how to post data in node.js with content type ='application/x-www-form-urlencoded'

How do I post data using okhttp library with content type x-www-form-urlencoded?

How to POST with application/x-www-form-urlencoded header and URLSearchParams using isomorphic-fetch

Swift - How to send POST request with "x-www-form-urlencoded" content-type

Swift 4 send POST request as x-www-form-urlencoded

How to post (x-www-form-urlencoded) Json Data using Retrofit?

How to post a x-www-form-urlencoded data properly using javascript?

How do I get raw request body using servicestack with content-type set to application/x-www-form-urlencoded?

How to send bearer token and x-www-form-urlencoded data using Node Request

How to POST content as application/x-www-form-urlencoded

How to POST x-www-form-urlencoded in retrofit

How to send a POST request with Content-Type "application/x-www-form-urlencoded"

How to post request with spring boot web-client for Form data for content type application/x-www-form-urlencoded

How to send post request with content-type x-www-form-urlencoded android retrofit

PYTHON: requests.post() how to send request_body encoded as application/x-www-form-urlencoded

How test Post request with custom object in content type application/x-www-form-urlencoded?

How to send post request with x-www-form-urlencoded body with loopj-async-http library

How to send x-www-form-urlencoded in a post request in webclient?

how to post body x-www-form-urlencoded using webclient?

How to sent Http POST request with application/x-www-form-urlencoded

How to do a x-www-form-urlencoded POST login using cypress?