Search code examples
javascriptapipoststrapi

How to add repeatable component in strapi via HTTP request


Initially, the messages field (a Strapi repeatable component) will look like this.

"messages":[
    {
        "from": "user1",
        "_id": "5f787348628eea0017b49f7e",
        "text": "happy hello world",
        "createdAt": "2020-10-03T12:49:12.052Z",
        "updatedAt": "2020-10-03T12:49:12.052Z",
        "__v": 0,
        "id": "5f787348628eea0017b49f7e"
    }
]

I want to add another message to the repeatable component:

"messages":[
    {
      "from": "user2",
      "text": "happy hello world"
    }
  ]

When I Put it via curl:

curl -i -X PUT \
   -H "Content-Type:application/json" \
   -d \
'{
  "messages":[
    {
      "from": "shop",
      "text": "happy hello world"
    }
  ]
}' \
 'https://myserver/mes/5f781bdd23e08f001732cdd8'

It overwrites the previous message. How do I add the other message without losing any previous data?


Solution

  • You have to send all the array with PUT like you did.

    {
        "id": "5f781bdd23e08f001732cdd8",
        "messages": [
            {
                "id": "5f787348628eea0017b49f7e",
                "from": "user1",
                "text": "happy hello world"
            },
            {
    
                "from": "shop",
                "text": "happy hello world"
            }
        ]
    }
    

    Looks like Strapi can't know if you want to update the first, or delete it, or create a new one ...