Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Strapi POST API for content-type with relational fields

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.

like image 257
kalexei Avatar asked Feb 06 '26 11:02

kalexei


2 Answers

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
        }
      }
    );
like image 200
kalexei Avatar answered Feb 08 '26 00:02

kalexei


i had similair problem and enabling find user under USERS & PERMISSIONS PLUGIN worked for me.

like image 35
IaziBashir Avatar answered Feb 08 '26 02:02

IaziBashir



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!