1

There is no problem with a node creation with filled in title/body/custom text field fields using REST API. We just need to send a post request with the following body:

{
  "_links": {
    "type": {
          "href": "http://localhost/drupal/rest/type/node/page"
    }
  },
  "title": {"value": "Does it work?"},
  "body": {"value": "node body"},
  "field_test": {"value":"hello"}
}

Where "field_test" is a plant text field.

But how to create a node with filled in tags field?

I've tried to post a node using the following post request body:

{
  "_links": {
    "type": {
          "href": "http://localhost/drupal/rest/type/node/page"
    }
  },
  "title": {"value": "Does it work?"},
  "body": {"value": "node body"},
  "field_tags": {"value":"tag1"}
}

It creates the node, but without tags ("field_tabgs") value. Any help?

2 Answers 2

1

They are relations, so firstly you need to create the tags with the taxonomy REST service.

/taxonomy/term

You must take into account they are entities ( node and taxonomy_term ) and the field_tags field only stores the reference to the entity taxonomy_term.

Once you have created it, you will have to add the tags in the following way in your node definition.

Don't forget change the term_id ( in my case 1 ) and the uuid :

{
  "_links": {
    "type": {
      "href": "http://localhost/drupal/rest/type/node/page"
     }
  },
  "title": {"value": "Does it work?"},
  "body": {"value": "node body"},
  "_embedded": {
    "http://localhost/drupal/rest/relation/node/page/field_tags": 
      [
        {
          "_links": {
          "self": {
            "href": "http://localhost/drupal/taxonomy/term/1?_format=hal_json"
          },
          "type": {
            "href": "http://localhost/drupal/rest/type/taxonomy_term/tags"
          }
        },
        "uuid": [
          {
            "value": "ff61ea71-2540-47fe-a4bb-384b12d4de47"
          }
        ],
       "lang": "en"
      }
    ]
  }
}

More information in the following link.

2
  • how to get the uuid ?
    – Zorox
    Commented Aug 31, 2020 at 9:45
  • If you don't get the uuid in the response when you have created the term, you can request to the server /taxonomy/term/1 to get all values. In the previous example, the term id = 1
    – Rufein
    Commented Aug 31, 2020 at 10:32
0

You can simply use

"field_tags": {"target_id":"46"} // where 46 is term id

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.