Reputation: 2599
I have a backend server and want to make a Http POST call with a Body Parameter.
I create a Post request having @Header with api key and @Body with user info. But when I hit the api, in response it shows a response like this "data":[] where it should be like "data":{}. But if I send multiple @Field as parameter, it will be okey. Is there any process to use @Body instead of multiple @Field
SignInInfo.kt
data class SignInInfo(
@SerializedName("name") val name: String,
@SerializedName("email") val email: String,
@SerializedName("user_type") val userType: String,
@SerializedName("phone") val phone: String,
@SerializedName("password") val password: String
)
api.kt
@POST(Constants.API_USER_REGISTRATION)
fun registerUser(
@Body signInInfo: SignInInfo
): Call<RegistrationResponse>
api.kt
@FormUrlEncoded
@POST(Constants.API_USER_REGISTRATION)
fun registerUser(
@Field("name") name: String,
@Field("email") email: String,
@Field("user_type") user_type: String,
@Field("phone") phone: String,
@Field("password") password: String
): Call<RegistrationResponse>
I got this expected answer while use @Field parameter
{
"status": 1,
"data": {
"id": 135386,
"user_id": 1567057386,
"token": "geYs2rO6wjtO653I9OVhJWTFwYxAvnYnLGG7wm7dfa"
},
"msg": "Welcome Aomi"
}
I got this unexpected answer while use @Body parameter
{
"status": 2,
"data": [],
"msg": "Failed"
}
Upvotes: 1
Views: 276
Reputation: 3097
Your Body data class should look like this
data class Body(
@SerializedName("status")
val status: Int? = 0,
@SerializedName("data")
val `data`: Data? = Data(),
@SerializedName("msg")
val msg: String? = ""
)
data class Data(
@SerializedName("id")
val id: Int? = 0,
@SerializedName("user_id")
val userId: Int? = 0,
@SerializedName("token")
val token: String? = ""
)
if you want to get this kind of json
{
"status": 1,
"data": {
"id": 135386,
"user_id": 1567057386,
"token": "geYs2rO6wjtO653I9OVhJWTFwYxAvnYnLGG7wm7dfa"
},
"msg": "Welcome Aomi"
and usage
@POST(Constants.API_USER_REGISTRATION)
fun registerUser(
@Body signInInfo: Body
): Call<RegistrationResponse>
Upvotes: 1
Reputation: 965
Yes you can do this like this
@POST(Constants.API_USER_REGISTRATION)
fun generateOTPForgotPassword(@Body signInInfo: SignInInfo): Call< RegistrationResponse>
Just like the way you did.
Upvotes: 0