0

Drupal 8.9.11

Image File Uploaded via REST gets "status - Temporary" while setting explicitly "status" case to True server error 403 (code in python)

   "status": [
      {
        "value": True
      }
   ],

The sever returns code 403, an exception:

Status: 403, Message: {"message":"Access denied on creating field \u0027status\u0027."}

The error on the server: $ vendor/bin/drush watchdog-show

access denied Warning Path: /themag_dp8/entity/file?_format=hal_json.
   Symfony\Component\HttpKernel\Exception\AccessDeniedHttpException: Access denied on creating
   field 'status'. in Drupal\rest\Plugin\rest\resou

If I'll not specify "status = true" The next REST call to Create Media fails to reference it due to its temporary status.

   payload = {
        "name": {
            "value": name
        },
        "field_image": [
            {
                "target_id": file_id
            }
        ],
        "field_description": [
            {
                "value": description
            },
        ],
        "bundle": [
            {
                "target_id": "image"
            }
        ],
        "_links": {
            "type": {
                "href": oauth.base_url + REST_TYPE_MEDIA_IMAGE
            },
            oauth.base_url + "/rest/relation/media/image/thumbnail": [
                {
                    "href": oauth.base_url + "/file/%d" % file_id + FORMAT_HAL_JSON
                }
            ],
            oauth.base_url + "/rest/relation/media/image/field_image": [
                {
                    "href": oauth.base_url + "/file/%d" % file_id + FORMAT_HAL_JSON
                }
            ]
        }
    }
Status: 422, Message: {"message":"Unprocessable Entity: validation failed.\nfield_image.0.target_id:
This entity (file: 119) cannot be referenced.\n"}

Server error: $ vendor/bin/drush watchdog-show

access denied Warning 
    Path: /themag_dp8/entity/paragraph?_format=hal_json.
    Symfony\Component\HttpKernel\Exception\AccessDeniedHttpException:  in
    Drupal\Core\Routing\AccessAwareRouter->checkAccess() (line 120 of
php           Error   
    Symfony\Component\HttpKernel\Exception\UnprocessableEntityHttpException: Unprocessable Entity:
    validation failed.
    field_image.0.target_id: This entity (file: 119) cannot be referenced.
    in

When I manually, via web site admin (https://drupal8.site/admin/content/files), change the file status to "Permanent", this Create Media REST call succeeds to create a new media/image entity.

Seems the Rest user missing some security allowances, but which one? Did anybody succeed to create a node consisting of the teaser image, paragraph video and so?

Sure I'm not the first who attack this issue, where the relevant documentation, working code examples?

2 Answers 2

0

The solution I found so far is to fix a Drupal module code:

modules\contrib\file_entity\src\Normalizer\FileEntityNormalizer.php on line 48 add:

$entity->setPermanent();

See:

0

You can upload images and create media without the File Entity module and any module fixes.

Try this:

New "file_upload" REST resource plugin: allows file uploads of any size, creates File entities

In your case, the request would be like this:

POST /file/upload/media/image/field_media_image?_format=hal_json HTTP/1.1
Content-Type: application/octet-stream
Content-Disposition: file; filename="filename.jpg"

[… binary file data …]

And request to create media /entity/media?_format=hal_json:

{
    "_links": {
        "type": {
            "href": "YOUR_SITE/rest/type/media/image"
        }
    },
    "name": [
        {
            "value": "Image.jpg"
        }
    ],
    "field_media_image": [
        {
            "target_id": YOUR_ID
        }
    ]
}

It's works fine.

Your Answer

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

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