Search code examples
kotlinretrofitmvpandroid-mvp

Error Code 415 with retrofit


I have created following API class to query the web service

 @POST("/v1/User/forgot-password")
    @FormUrlEncoded
    fun forgotPass(@Field("email") email: String): Call<ForgotPassword>

and called it as

 val call = RetrofitHelper.instance!!.api.forgotPass(email)
        call.enqueue(object : Callback<ForgotPassword> {

            override fun onResponse(call: Call<ForgotPassword>, response: Response<ForgotPassword>) {
                if (response.code() == 200) {
                    Log.e("response", response.body().toString())
                    forgotPassView.forgotPassRequestSuccess()

                }
            }

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

The response I am getting is 415, while web services are working fine.

I have tried different techniques to resolve it but it seems there are something more to it. Please help me solve this issue. Thanks.


Solution

  • To send a json payload, define a wrapper class --

    data class EmailBody(val email: String)
    

    and use that as the @Body for your POST

    @POST("v1/User/forgot-password")
    fun forgotPass(@Body email: EmailBody): Call<Void>
    

    Then, when you want to call it, wrap the email address in an EmailBody object --

    service.forgotPass(EmailBody(email))