Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Retrofit response code 405 with message "method not allowed here"

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")
        }
    })
like image 959
jemo mgebrishvili Avatar asked Nov 16 '16 17:11

jemo mgebrishvili


2 Answers

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/
like image 196
Abbas Khan Waliullahi Avatar answered Sep 21 '22 07:09

Abbas Khan Waliullahi


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

like image 41
B.shruti Avatar answered Sep 21 '22 07:09

B.shruti