Score:1

?_format=json does not produce JSON for files, REST

us flag
J.g

I am using Drupal 9.3 with REST UI, RESTful Web Services, and Serialization enabled. For the REST UI, I have the following configuration for files:

Methods: GET, POST, DELETE, PATCH

Accepted request formats: json, xml

Authentication providers: cookie,

but when I try to get a json for a file (by appending ?_format=json to the url), I just get the file itself back (in other words the page is the same as without ?_format=json). This only happens for files, both media and nodes generate the json as usual.

Has anybody experienced this or knows how to fix it?

J.g avatar
us flag
J.g
For example, when I have some media, at say `baseurl/media/1`, I can get a json by changing the url to `baseurl/media/1?_format=json`, similarly with nodes, `baseurl/node/1?_format=json`, I would like to do the same thing with files like `baseurl/sites/default/files/2021-06/test_0.txt`
Score:1
in flag

This is because Drupal delivers the file from filesystem without any processing.

If you want a file delivered as JSON (whatever that means). You need a controller that loads the file, then does a json_decode on the content and returns the result as Symfony\Component\HttpFoundation\JsonResponse.

And you also need a route for that controller method.

J.g avatar
us flag
J.g
I quite new to Drupal, could you provide some guidance on how to go about doing that (creating a controller and route)
Rainer Feike avatar
in flag
You can find all of this, including code examples at drupal.org, i.e. here: https://www.drupal.org/docs/drupal-apis/routing-system/introductory-drupal-8-routes-and-controllers-example
Score:0
cn flag

You cannot add ?_format=json to a file url at www.example.com/sites/default/files because that is just the directory of site files.

So why does Drupal give you json when you add ?_format=json to www.example.com/node/123?_format=json or www.example.com/media/123?_format=json? That's because these are entities.

So, how can you use ?_format=json with files? You'll need to use entities.

The easiest way to do this is with the Media module, which is built into core.

To try this out:

  1. Install the media module.
  2. Go to /admin/structure/media.

By default, some basic media types will be created (audio, document, image, remote video, and video).

Check out the Doument media type as it is a good example of using files.

You can also easily create new media types for your specific use case.

Then you can use ?_format=json on the media URLs that you create.

The downside of this approach is that you still can't get the json from the file directory path; you will have to use the media entity url instead.

To set the media entity urls automatically based on a pattern, you can use Pathauto.

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.