Score:0

Creating content on via REST API using POST: proper formatting of json?

kr flag

I'm trying to do a POST to create content on my site, but I get an Access Denied for creating field: changed even though I am an administrator role. When I remove the "changed" field in the JSON I get the Website encountered an unexpected error error. I assume my JSON format is incorrect. I got it from doing a GET on one of the published nodes, and just removed the nid and uuid values in the JSON.

Or is there a better way to get the proper format I need to create content or am I including too many fields in the JSON?

curl -u xxxxxx:yyyyyy -X POST \
  -H "Content-Type: application/json" \
  -d '{
    "vid": [{"value": 349}],
    "langcode": [{"value": "en"}],
    "type": [{"target_id": "chats", "target_type": "node_type", "target_uuid": "1332c2f2-ecce-49fd-bff4-4bf37dd24f7b"}],
    "status": [{"value": true}],
    "uid": [{"target_id": 1, "target_type": "user", "target_uuid": "0e0c2c1d-ed85-4152-bbd6-3821005b07fd", "url": "\/user\/1"}],
    "title": [{"value": "tessssst"}],
    "created": [{"value": "2023-03-15T17:36:47+00:00", "format": "Y-m-d\\TH:i:sP"}],
    "changed": [{"value": "2023-05-09T19:40:40+00:00", "format": "Y-m-d\\TH:i:sP"}],
    "promote": [{"value": false}],
    "sticky": [{"value": false}],
    "default_langcode": [{"value": true}],
    "revision_translation_affected": [{"value": true}],
    "path": [{"alias": null, "pid": null, "langcode": "en"}],
    "publish_on": [],
    "unpublish_on": [],
    "body": [{"value": "<p>this is a test!<\/p>", "format": "full_html", "processed": "<p>this is a test!<\/p>", "summary": ""}],
    "field_chat_parent": [],
    "field_page": [{"value": 2}]
  }' \
  https://xxxx.yyy/node?_format=json

enter image description here

Kevin avatar
in flag
Are you passing a cookie or authenticated token?
Patoshi パトシ avatar
kr flag
using basic authentication. created a user for it.
Kevin avatar
in flag
Yes but are you passing that _back_ with each request? This is required with curl. I do not see that in your request.
Patoshi パトシ avatar
kr flag
its in the first line "-u " --- i have it blanked out in red.
Patoshi パトシ avatar
kr flag
I'm testing on just the "basic page" and i still get this error: **{"message":"Access denied on creating field \u0027changed\u0027."}** -- if i remove the "changed" field from POST, it shows a website has encountered an error.
Score:2
kr flag

After digging around a few hours. This answer helped: https://drupal.stackexchange.com/a/283883/12585 -- Many of the answers as of 2023-May on the internet is for Drupal 8 and some of the parameters are deprecated like hal+json. This works for Drupal 9.4 as of today.

I managed to first create a node using the most simplified amount of variables:

curl -u myuser:password -X POST \
  -H "Content-Type: application/json" \
  -d '{
  "type":[{"target_id":"page"}],
  "title":[{"value":"Hello World xxxx221"}],
  "body":[{"value":"<p>this is a test!<\/p>", "format": "full_html"}]
}' \
  https://xxxxxxxxx.com/node?_format=json

Pasting the above into the command line will just work and won't throw an error. The error I ran into was due to the fact I was copying all the json output from the initial query and pasting everything to the POST. Some of the things will need to be changed to make it work with POST for Drupal 9.

I sit in a Tesla and translated this thread with Ai:

mangohost

Post an answer

Most people don’t grasp that asking a lot of questions unlocks learning and improves interpersonal bonding. In Alison’s studies, for example, though people could accurately recall how many questions had been asked in their conversations, they didn’t intuit the link between questions and liking. Across four studies, in which participants were engaged in conversations themselves or read transcripts of others’ conversations, people tended not to realize that question asking would influence—or had influenced—the level of amity between the conversationalists.