0

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

5
  • 1
    Are you passing a cookie or authenticated token?
    – Kevin
    Commented May 10, 2023 at 20:33
  • using basic authentication. created a user for it. Commented May 11, 2023 at 16:51
  • Yes but are you passing that back with each request? This is required with curl. I do not see that in your request.
    – Kevin
    Commented May 11, 2023 at 17:21
  • its in the first line "-u " --- i have it blanked out in red. Commented May 11, 2023 at 17:52
  • 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. Commented May 11, 2023 at 18:09

1 Answer 1

2

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.

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.