Problem/Motivation

My user accounts have an image file field (image, not media). I want to update the alt text of the image via JSON:API.

How can I do that?

Steps to reproduce

I tried

        const myJsonData : InterfaceJsonUserDataOnlyAltText = {
          data: {
            id: userObject.id,
            type: 'user--user',
            relationships: {
              field_ref_image: {
                data: {
                  id: fileUuid,
                  type: 'file--file',
                  meta: {
                    alt: userData.my_alt_text,
                  },
                },
              },
            },
          },
        };

However, when I post with axios, I get this error:

Symfony\Component\HttpKernel\Exception\UnsupportedMediaTypeHttpException: No route found that matches "Content-Type: application/vnd.api+json" in Drupal\Core\Routing\ContentTypeHeaderMatcher->filter() (line 49 of /app/web/core/lib/Drupal/Core/Routing/ContentTypeHeaderMatcher.php).

Comments

ptmkenny created an issue. See original summary.

Version: 9.2.x-dev » 9.3.x-dev

Drupal 9.2.0-alpha1 will be released the week of May 3, 2021, which means new developments and disruptive changes should now be targeted for the 9.3.x-dev branch. For more information see the Drupal core minor version schedule and the Allowed changes during the Drupal core release cycle.

bbrala’s picture

Status: Active » Postponed (maintainer needs more info)

If you want to update data on a related object you should post to the endpoint of that specific object. This would probably be:


const myJsonData : InterfaceJsonUserDataOnlyAltText = {
          data: {
            id: fileUuid,
            type: 'file--file',
            data: {
              meta: {
                alt: userData.my_alt_text,
              },
            },
          },
        };

When updating relations in an endpoint you can only update the relationsship properties, not the data of the related resource.

Does this fix your issue?

bbrala’s picture

Status: Postponed (maintainer needs more info) » Needs review
bbrala’s picture

Status: Needs review » Fixed

As we have no activity on this support request im going to assume the issue fixed.

wim leers’s picture

Crediting you, @bbrala :) ;)

wim leers’s picture

(Based on the error, I'd say they were indeed sending the request to the wrong route/URL.)

ptmkenny’s picture

Thanks @bbrala, I didn't realize I needed to post to the object's endpoint.

I'm sorry to be late getting back to the issue, but this can definitely be closed.

wim leers’s picture

Lovely, thank you!

bbrala’s picture

Status: Fixed » Closed (fixed)

Automatically closed - issue fixed for 2 weeks with no activity.

hlopes’s picture

Can I get some aditional clarification here?

Tried post, put, patch with the following data

{
  "data": {
    "id": [file_uuid], 
    "type": "file--file", 
    "data": {
      "meta": {
        "alt": "ALT", 
        "title": "TITLE"
      }
    }
  }
}

to

/jsonapi/[entity_type]/[bundle]/[entity_uuid]/relationships/[field_name]

But the best I got was a 409 "You can only POST to to-many relationships"

Using PATCH seems to work but the fields (alt + title) aren't updated.

vincenzocacciatore’s picture

I am sorry but it looks like the meta field of an image is property of the node and not of the file.
Could you please help me to solve how to update alt field of an image?