Home > Net >  Strapi POST api for content-type with relational fields
Strapi POST api for content-type with relational fields

Time:02-26

Every time I try to create an order via /api/orders it gives me 400 (Bad request), there doesn't seem to be a proper clear explanation anywhere on how to create records with relational fields, the only one I found close to what I needed was this: Strapi "Create an entry" docs with relational fields

So supposedly I should use an id or a list of ids depending on the type of relation, but it still gives me 400 Bad request with no explanation in the response.

My order content-type looks like this:

Order content-type

User is a Many-to-One relationship, so a user can have many orders, but there can only be one user per order, and products is One-to-Many, so an order can have many products

This is what my API call looks like:

    await axios.post(
      `${baseUrl}/api/orders`,
      {
        products: [9],
        total: 320,
        user: 42
      }
    );

The products and user ids are exactly the ones I have in the database and authentication is not the problem.

Please help me understand what I'm doing wrong and how I should be creating records with relational fields. Thanks

CodePudding user response:

I think the problem is from your route, accessing it with ${baseurl}/orders should work fine.

CodePudding user response:

Ok, apparently it does give me a proper response describing the problem, it's just I had to go to the Network tab in the browser and find it there, and it's the stupidest thing, all I had to do it is this:

    await axios.post(
      `${baseUrl}/api/orders`,
      {
        data: {
          products: [9],
          total: 320,
          user: 42
        }
      }
    );
  • Related