We have a content-type w/ a File field. We would like to PATCH a node of that type to update only the File field with a newly uploaded file.

We've been able to figure out how to successfully POST a new file to the server, so now we only need to update the node to point to the file (that is already on the server now). It seems we should use a PATCH on the node, passing in just the data pertaining to the File field with the updated path to the file -- but no matter how we try, we always get back errors (usually 400).

It seems likely that we're just not formatting the data in the request right. What is the proper format for patching a node to update a File field? Or is this an unsupported feature?

Thanks for any help you can provide!

Comments

cmoesel created an issue. See original summary.

grimreaper’s picture

Hello,

Did you use the JSONAPI file module? I got the following similar (same?) problem. Please see #2897545: Support PATCH operation

cmoesel’s picture

I don't think the problem described in #2897545 is the same, but it's possible that they share a root cause.

The main difference is that #2897545 seems to be trying to PATCH the file entity itself. We, however, are trying to PATCH a node that has a File field -- so we're not PATCHING a file entity, but rather, we're trying to associate a new file entity (which we already created via POST) to a File field on an existing node. We can't seem to figure out how to PATCH the node to reference the file entity.

cmoesel’s picture

Is anyone able to confirm or deny if this is even intended to work (or known to work under any circumstances)?

wim leers’s picture

We've been able to figure out how to successfully POST a new file to the server

Really? 😲

How?

Because neither Drupal core's REST module nor JSON API supports that today. For Drupal core's REST module, we have #1927648: Allow creation of file entities from binary data via REST requests. And JSON API is effectively blocked on that issue to support it.

wim leers’s picture

Category: Support request » Bug report
Issue tags: +Needs tests

Oh … you're able to POST a file by using https://www.drupal.org/project/jsonapi_file, which is a work-around until #1927648: Allow creation of file entities from binary data via REST requests happens.

If you could write a failing test, that'd be super helpful. Do something like:

function setUp() {
  $file_1 = File::create(…);
  $file_2 = File::create(…);

  $node = Node::create([
    …
    'file_field' => [
      'target_id' => $file_1->id(),
    ],
    …
  ]);
}

function testPatchFileField() {
  // use JSON API to change the target ID from $file_1->id() to $file_2->id()

  // this should FAIL
}

It'd be good to have explicit test coverage for this common use case anyway.

wim leers’s picture

wim leers’s picture

Status: Active » Postponed (maintainer needs more info)
wim leers’s picture

wim leers’s picture

Status: Postponed (maintainer needs more info) » Closed (duplicate)