kalexei
kalexei

Reputation: 125

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.

Upvotes: 3

Views: 8823

Answers (4)

IaziBashir
IaziBashir

Reputation: 21

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

Upvotes: 1

gil.fernandes
gil.fernandes

Reputation: 14621

In the case you cannot update a reference even with the right JSON structure like the one mentioned here, then check the permissions given to the referenced entity.

Check that the update and create permissions are granted like here:

enter image description here

Upvotes: 0

kalexei
kalexei

Reputation: 125

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
        }
      }
    );

Upvotes: 3

nasirmustapha
nasirmustapha

Reputation: 171

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

Upvotes: 0

Related Questions