'Sending Data in Body in retrofit kotlin

I am using retrofit for calling the API i have to send the data in Body as we send in Java @Body but don't know how to parse the data...

 {
 "appType": "EXTERNAL",
 "appDetails":{
   "os": "MAC_OSX",
   "osVersion": "1.2",
   "appVersion": "1.0",
   "deviceFamily": "MOBILE",
   "ipAddress": "192.168.5.2"
 },
 "consumerSections":[
   "Support",
   "English",
   "other"
 ],
 "engagementAttributes": [
   {
     "type": "personal",
     "personal": {
       "contacts": [{"email":"test.com","phone":"12345678"},{"email":"test2.co.il","phone":"98765430"}],
       "age": {
         "age":30.0,
         "year":1985,
         "month":7,
         "day":22
       },
       "firstname": "test",
       "lastname": "test2",
       "gender": "FEMALE",
       "company": "liveperson"
     }
   }
 ]
}

in the API call, how can i parse it and send data to the server. Please tell....



Solution 1:[1]

If you need to send plain json, you could do:

@Headers("Content-Type: application/json")
@POST("login")
fun getUser(@Body body: String) : Call<User>

If you want automatically cast you kotlin data class, add

compile 'com.squareup.retrofit2:retrofit:2.3.0'
compile 'com.squareup.retrofit2:converter-gson:2.3.0'

And then, just set your object with @Body annot.

@Headers("Content-Type: application/json")
@POST("login")
fun getUser(@Body body: YourCustomDataObject) : Call<User>

Here is an example how to connect all your retrofit2 and service interface together.

Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source
Solution 1 Dmytro Ivanov