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")
}
})
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")