How to create relationship through RestAPI

System Information
  • 4.10.6: Strapi
  • Window 8.1: OS
  • SQLite: Database
  • 18.x.x: Node
  • 9.6.7: NPM
  • Yarn Version:

How to create relationship through RestAPI

I have this kind of relationship, which is many to one.

My question is how can I create relationship from rest api.
I can create an article, but I can’t create an article with author id 1.
My http used is the following.


POST http://localhost:1337/api/articles
Authorization: Bearer eyJhbGciOiJIUzI1NiIsInR5cCI6IkpXVCJ9.eyJpZCI6MSwiaWF0IjoxNjg1NDM1Nzc1LCJleHAiOjE2ODgwMjc3NzV9.nZnoLQGm2N3RScXp1hCHyCWzhq1ARg06Bnpuo55aoHs
Content-Type: application/json

{
  "data":{
    "article_intro":"12 article",
    "article_body":"From author from http",
    "author":1
  }
}

Did you try this? … Relations | Strapi Documentation

Strapi relationships are not - it seems - ‘fields’ in a content type (as you might expect if you’re used to relational databases), but an array of links that must be set up or modified as ‘connections’. And if you use relationships, you then need to ‘populate’ them if you want to see them in API results.

I used the following code according to the article link you share.

### Put Article
PUT http://localhost:1337/api/users/2
Authorization: Bearer eyJhbGciOiJIUzI1NiIsInR5cCI6IkpXVCJ9.eyJpZCI6MSwiaWF0IjoxNjg1NDM1Nzc1LCJleHAiOjE2ODgwMjc3NzV9.nZnoLQGm2N3RScXp1hCHyCWzhq1ARg06Bnpuo55aoHs
Content-Type: application/json

{
  "data":{
    "articles":{
      "connect":[4,6]
    }
  }
}

Still not working!

Anyone has any update on this issue? I am having same issue, i tried to create an article with category relationship using the API, but the category can not be set, i am following the v4 ApI doc here:

but apparently something is missing or off

I think i found the reason now, the api user needs to be granted permission to manage the, e.g. the category content type too in my case…however, still , the API reaponse 200 ok is misleading, if it is a permission issue, though not directly at the article cotent type, but a related one, it still need to throw a permission denied response rather than 200ok