'How to POST Object Array with Retrofit?
I send the data from the postman like this:
{"contacts":[
{
"idContact": "201",
"name": "Joseph",
"lastName": "Herrera",
},
{
"idContact": "202",
"name": "Lizzy",
"lastName": "Martinez",
}
]}
And it works correctly but I tried to POST from retrofit like this:
@FormUrlEncoded
@POST("saveContacts")
fun saveContact(
@HeaderMap headers: Map<String, String>,
@Field("contacts[]") contacts: List<Contact>
): Call<Response>
And this way:
@POST("saveContacts")
fun saveContact(
@HeaderMap headers: Map<String, String>,
@Body contacts: List<Contact>
): Call<Response>
But both ways doesn't work for me. i get a bath response from API:
local.ERROR: Illegal string offset 'idContact' {"userId":1,"exception":"[object] (ErrorException(code: 0): Illegal string offset 'idContact' at
or
local.ERROR: Argument 1 passed to App\Services\ListingService::create() must be of the type array, null given
respectively.
Contacts is a class with the field specified. I send the Info to retrofit Function like this:
val contacts= listOf(contact1,contact2)
RetrofitClient.instance.saveContact(
headers = headerMap,
contacts = contacts
)
What is the correct way to post this kind of data?
Sources
This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.
Source: Stack Overflow
Solution | Source |
---|