Search code examples
androidkotlinretrofit2

I have null field but retrofit dont send to backend


I have class like this:

class MyData {

    @SerializedName("name")
    var name: String? = null

    @SerializedName("surName")
    var surName: String? = null
    
}

My service calls is like this.

interface MyServiceCall {
    
    @PUT("/My/endPoint")
    fun updateInfo(@Body info: MyData): Flowable<Response<ResponseBody>>

}

But when I want to send only name and surname null. The null dont go to backend and then return 400 - Bad Request.

So my question is How can I send data to backend even fields are null?


Solution

  • Apply serializeNulls() in GsonBuilder() when adding converterFactory for Retrofit object :

     retrofit = new Retrofit.Builder().baseUrl("YOUR BASE URL")
        .addConverterFactory(GsonConverterFactory.create(new GsonBuilder().serializeNulls().create()))
        .build();