Getting content with REST - examples
Mozilla Firefox includes a JSON viewer. We suggest you use Firefox to view the links below in a more readable format.
Look at the following examples of how to get content from our public demo using our out-of-the-box REST API.
Get one specific item using the default delivery endpoint. In this example we get a specific tour published to our public demo site by providing the path:
Get all content from a sub-resource using the default delivery endpoint. In this example we get all the tours published to our public demo site:
Run a fulltext search for the word "landscape" in all the tours published to our public demo site using the default delivery endpoint:
Filter on the
Get tour content limited to five results and and offset to begin at number ten using the default delivery endpoint:
Setting up Magnolia for REST
The latest version of Magnolia is 5.6.1 so you're right to wonder.
Currently in Magnolia, we only support a single delivery endpoint configuration. In Magnolia 5.6.1, a preconfigured endpoint is delivered by default making it a little difficult to create your own endpoint later on in this tutorial. Magnolia 5.6.0 does not deliver a preconfigured endpoint.
Soon this point will be moot as we plan to support multiple endpoints. Keep an eye on MGNLREST-152 - Getting issue details... STATUS to see how development is coming along!
If you do not have Magnolia CLI, install it as described here.
Then install Magnolia as follows:
Change to the directory to where you want to install the Magnolia bundle. In our example, we use the directory
Execute the Magnolia CLI
jumpstartcommand to get Magnolia 5.6.0:
Jumpstart downloads and extracts the
magnolia-community-demo-bundle5.6.0 that comes with Tomcat server.
The following files and folders are created:
Go to the
~dev/mgnl-rest-test-basedirectory and execute the Magnolia CLI
When starting for the first time, Magnolia runs a web update and automatically installs all its modules.
In your preferred browser, open the URL http://localhost:8080/magnoliaAuthor/ and log in as user
superuserwith the password
Have a look around Magnolia. Access the public instance with the URL http://localhost:8080/magnoliaPublic/.
REST endpoints are a powerful tool but can also make your site very vulnerable. Make sure you understand how to implement a strong security strategy to safeguard your system..
In the context of this tutorial and to get started quickly, we use users with roles provided by the default setup of the Magnolia bundle:
superuserin the author instance has:
- Read/Write access for the path
/on every JCR workspace, granted by the
Web access for the HTTP methods GET, PUT, POST and DELETE for the path
/magnoliaAuthor/.rest*granted by the role
- Read/Write access for the path
anonymous(unauthenticated user) in the public instance has:
Read access on the path
/for the JCR workspaces
Web access for the HTTP method
GETfor the path
Before enabling and using the REST endpoints in a production environment, y ou must read and understand REST security .
Using the delivery endpoint
The delivery endpoint is a REST API provided by Magnolia out-of-the-box. Use it for obtaining JCR data as JSON.
You must provide YAML-based configuration for the delivery endpoint so that it can serve JSON.
We will create a light module to provide the YAML-based configuration required for the delivery endpoint.
In your light-modules folder, which is configured with the property
magnolia.resources.dir, create the following structure:
You can use this code to start with for the
For the time being, note the following points:
- Line 3:
websiteis the name of the endpoint prefix. In our case it is also the name of a JCR workspace. We use the value of the endpoint prefix later on in the REST request URL.
- Everything below line 3 defines the endpoint prefix.
- You can define more endpoint prefixes.
- Make sure you only have one YAML-based endpoint definition.
Reading website content with the delivery endpoint
With the configuration provided above, you are ready to send REST requests to your Magnolia instance.
We will fetch the content of the page
/travel/about on the website workspace. Have a look at Delivery endpoint API - readNode to understand how to compose the URL. We need:
- The name of endpoint prefix =>
- The relative path of the node - relative to what is defined in the configuration as rootPath: =>
Now add the "context" (the name of the webapp), the domain and the protocol, and you get these URLs:
You can request the first URL, which goes to the public instance, with the browser as the anonymous user (without authentication). For the second URL, you must authenticate.
To test these URLs with cURL, use the following commands:
REST endpoints overview
Magnolia provides the following REST endpoints out-of-the-box:
|Endpoint||HTTP methods||Swagger UI enabled|
|Read node||Create node||Update node||Delete node|
|Read property||Delete property|
If you want to use REST to create, update and delete content, we recommend you use the Nodes endpoint which supports all required operations. If you mainly want to read data, consider using the Delivery endpoint. It provides convenient, formatted JSON and can be customized and configured with YAML via light module. With the Commands endpoint you can trigger commands and Cache endpoint deals with cache.