Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to pass a key with null value or an empty String in the request body in retrofit android

I need to pass the keys even with no values as the keys are mandatory at the server side. But Retrofit was removing the keys with null values while sending the request. How I can achieve sending keys without values to the server with the use of Retrofit?

Request body look like this :

{
        "first_name":"testlogin",
        "last_name":"lastname",
        "username":"testman", 
        "password":"test123", 
        "email":"[email protected]", 
        "address1":"123+test+way",
        **"address2":"",**
        **"address3":"",**
        "postal_code":"75023",
        "country":1,
        **"state":""**
}

Thanks in advance.

like image 517
NagarjunaAlaparthi Avatar asked Oct 15 '16 14:10

NagarjunaAlaparthi


1 Answers

Try passing it as:

"address2":null,
"address3":null,
"postal_code":"75023"
"country":1,
"state":null

Since you are using Gson converter, try creating the gson as:

Gson gson = new GsonBuilder().serializeNulls().create();

from here.

like image 129
Nongthonbam Tonthoi Avatar answered Sep 19 '22 09:09

Nongthonbam Tonthoi