How to add new post (coin) containing a component (buys) with POST request

System Information
  • Strapi Version:
  • Operating System:
  • Database:
  • Node Version:
  • NPM Version:
  • Yarn Version:

First, an exemple of a coin added in strapi admin. url: http://localhost:1337/coins/1

{
  id: 1,
  name: "bitcoin",
  symbol: "btc",
  created_at: "2021-08-11T23:38:11.694Z",
  updated_at: "2021-08-13T21:10:16.681Z",
  buys: [{
    0: {
      id: 5,
      amount: 0.02,
      price: 40000
    }
  }]
}

I made a front-end form to add new coin. Below is the part of the function where I insert input datas. I tested with only name and symbol and it works. But when I try to create new coin with buys datas (which is a repeatable component) I cant figure out how to write request body? Check commented lines.

axios.post("http://localhost:1337/coins", {
  name: post.name,
  symbol: post.symbol,
  buys: [{
    0: {
      amount: post.amount, // --> response returns null
      price: post.price, // --> response returns null
    },
  }]
})

Thanks for your help.

you shouldn’t include the 0: { part meaning your body should be something like:

axios.post("http://localhost:1337/coins", {
  name: post.name,
  symbol: post.symbol,
  buys: [{
      amount: post.amount,
      price: post.price,
    }]
})
1 Like

I have a similar problem, but I want to send (POST):

My API looks like this:

{
  "data": [
    {
      "id": 3,
      "attributes": {
        "nome": "Pedro",
        "createdAt": "2022-06-02T03:10:54.930Z",
        "updatedAt": "2022-06-02T03:11:07.524Z",
        "publishedAt": "2022-06-02T03:11:07.522Z",
        "especialidade": [
          {
            "id": 7,
            "nome_especialidade": "Cirurgião"
          },
          {
            "id": 8,
            "nome_especialidade": "Pediatra"
          }
        ]
      }
    }
  ],
  "meta": {
    "pagination": {
      "page": 1,
      "pageSize": 25,
      "pageCount": 1,
      "total": 1
    }

I’m trying to use fetch to send data from a form like this:

fetch(`http://localhost:1337/api/profissionals`, {
      method: 'POST',
      body: JSON.stringify({
        data: {
          nome,            
        },
        especialidade: [{
          nome_especialidade
        }]       
    }),
    headers: {
      'Content-Type': 'application/json'
    }  
    }).then(() => {
      window.location.reload(false);
    })

So, the specialties are not being registered. What am I doing wrong?