I have null field but retrofit dont send to backend

Viewed 1372

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?

1 Answers

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();
Related