skip to Main Content

I am using retrofit and I have a post request

interface NetworkApi {

@Headers("Content-Type: application/json")
@POST("/")
fun startLoginRequest(@Body loginModel : LoginModel) : Call<BaseResponseModel>

class Factory {
    var retrofit = Retrofit.Builder()
            .baseUrl(BASE_URL)
            .addConverterFactory(GsonConverterFactory.create())
            .build()

    companion object{
        fun getApi (): NetworkApi {
            val serverApi = NetworkApi.Factory()
            val api = serverApi.retrofit.create(NetworkApi::class.java)
            return api
        }
    }
}

}

when I use this method and on response method were calling response body is always null.

fun startLoginRequest(){
    val api = NetworkApi.Factory.getApi()
    val request = api.startLoginRequest(loginRequestModel)

    request.enqueue(object : Callback<BaseResponseModel>{
        override fun onFailure(call: Call<BaseResponseModel>?, t: Throwable?) {

        }

        override fun onResponse(call: Call<BaseResponseModel>?, response: Response<BaseResponseModel>?) {
            //here response.body is null
        }
    })
}

Strange thing is that, if I will send the same object using Postman everything works fine

this is httpClient interceptor log

--> POST http://myExampleHost.net/ http/1.1
Content-Type: application/json

Content-Length: 95
Authorization: auth-value
--> END POST (95-byte body)
<-- 405 Method Not Allowed http://myExampleHost.net/ (198ms)
Allow: GET, HEAD, OPTIONS, TRACE, COPY, PROPFIND, LOCK, UNLOCK
Content-Type: text/html
Server: Microsoft-IIS/7.5
X-Powered-By: ASP.NET
X-Powered-By-Plesk: PleskWin
Date: Thu, 17 Nov 2016 08:04:57 GMT
Content-Length: 1107
<HTML>
<HEAD>
<TITLE>405 Method Not Allowed</TITLE>
<BASE href="/error_docs/"><!--[if lte IE 6]></BASE><![endif]-->
</HEAD>
<BODY>
<H1>Method Not Allowed</H1>
The HTTP verb used to access this page is not allowed.<P>
<HR>
<ADDRESS>
Web Server at example address
</ADDRESS>
</BODY>
</HTML>

For now I’m using OkHttp RequestBuilder and everything works fine, I don’t realize what I’m missing in the above example

val client = OkHttpClient()
    val JSON = MediaType.parse("application/json; charset=utf-8")
    val body = RequestBody.create(JSON,json)
    val request1 = Request.Builder()
            .url("http:example.com")
            .addHeader("content-type", "application/json")
            .method("POST", body)
            .build()

    client.newCall(request1).enqueue(object : okhttp3.Callback{
        override fun onFailure(call: okhttp3.Call?, e: IOException?) {

        }

        override fun onResponse(call: okhttp3.Call?, response: okhttp3.Response?) {
            response?.body()?.string()?.print("RETROFIT response")
        }
    })

6

Answers


  1. Chosen as BEST ANSWER

    I found the answer, the problem was url, my baseUrl were http//www.example.com/url1/url2/...../service and @POST("/")

    when I saw the POST endpoint it was http//www.example.com/ and I don't know why

    I just changed this urls baseUrl = http//www.example.com/ and @POST("url1/url2/...../service")


  2. My best guess is that you are trying to post to the wrong url www.example.com/login// instead of www.example.com/login/. APIs can be a bit finicky about posts.

    You can add the logging interceptor so you can see what you are posting and to what URL so its a little easier to debug. To set it up you need to add compile "com.squareup.okhttp3:logging-interceptor:$okhttp_version" to your gradle file and change your retrofit setup to something like this:

    val httpClient = OkHttpClient.Builder().apply {
        if (BuildConfig.DEBUG) {
            httpClient.addInterceptor(HttpLoggingInterceptor().apply {
                level = HttpLoggingInterceptor.Level.BODY
            })
        }
    }
    
    var retrofit = Retrofit.Builder()
            .client(httpClient.build())
            .baseUrl(BASE_URL)
            .addConverterFactory(GsonConverterFactory.create())
            .build()
    

    That’s the best advice I can give without seeing the error logs.

    Login or Signup to reply.
  3. I was missing “/” at the end of my url in the POST request.
    Changing url from

    http://www.example.com/list_products

    to http://www.example.com/list_products/ worked for me.

    Login or Signup to reply.
  4. Method not allowed error with status code-405 is showed when we are not sending our request method or wrong request method is getting sent.

    In my scenario,Api is of POST type and I was checking for GET request type,

    So try change your proper Request method(GET/POST/PUT/DELETE).

    Login or Signup to reply.
  5. For me the main reason was i was calling http instead https
    For Example:
    i was using this url as a base url which give me this error

    http://www.facebook.com/
    

    when i changed it to this one it worked like a charm

    https://www.facebook.com/
    
    Login or Signup to reply.
  6. For me instead of /api/login, /api/login/ fixed the issue

    Login or Signup to reply.
Please signup or login to give your own answer.
Back To Top
Search