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!