Blog API Basics
Liferay’s REST APIs provide services for adding, modifying, and deleting blog posts and images.
Liferay DXP 2024.Q4+/Portal 7.4 GA129+ External Reference Codes (ERCs) are accessible for blog posts and images, providing a consistent way to identify and access these elements across Liferay.
Start by seeing an example of adding a new blog post.
Adding a Blog Post
Start a new Liferay instance by running
Sign in to Liferay at http://localhost:8080. Use the email address test@liferay.com and the password test. When prompted, change the password to learn.
Then, follow these steps:
-
Download and unzip Blog API Basics.
-
Find your site’s ID. It’s used in different service calls below.
-
Use the cURL script to add a new blog post to your site. On the command line, navigate to the
curl
folder. Execute theBlogPostings_POST_ToSites.sh
script with your site ID as a parameter.The JSON response shows a new blog post has been added:
-
Go to the Blogs application by navigating to Administration Menu → Content & Data → Blogs. See that a new blog post has been added.
-
The REST service can also be called with a Java class. Navigate out of the
curl
folder and into thejava
folder. Compile the source files: -
Run the
BlogPostings_POST_ToSites
class. Replace thesiteId
value with your site ID:
Examine the cURL Command
The BlogPostings_POST_ToSites.sh
script calls the REST service with a cURL command.
Here are the command’s arguments:
Arguments | Description |
---|---|
-H "Content-Type: application/json" | Indicates that the request body format is JSON. |
-X POST | The HTTP method to invoke at the specified endpoint |
"http://localhost:8080/o/headless-delivery/v1.0/sites/${1}/blog-postings" | The REST service endpoint |
-d "{\"articleBody\": \"Foo\", \"headline\": \"Able\"}" | The data you are requesting to post |
-u "test@liferay.com:learn" | Basic authentication credentials |
Basic authentication is used here for demonstration purposes. For production, you should authorize users via OAuth2. See Using OAuth2 to Authorize Users for a sample React application that uses OAuth2.
The other cURL commands use similar JSON arguments.
Examine the Java Class
The BlogPostings_POST_ToSites.java
class adds a blog post by calling the blog posting related service.
This class invokes the REST service using only three lines of code:
Line (abbreviated) | Description |
---|---|
BlogPostingResource.Builder builder = ... | Gets a Builder for generating a BlogPostingResource service instance. |
BlogPostingResource blogPostingResource = builder.authentication(...).build(); | Specifies basic authentication and generates a BlogPostingResource service instance. |
BlogPosting blogPosting = blogPostingResource.postSiteBlogPosting(...); | Calls the BlogPostingResource.postSiteBlogPosting method and passes the data to post. |
Note that the project includes the com.liferay.headless.delivery.client.jar
file as a dependency. You can find client JAR dependency information for all REST applications in the API explorer in your installation at /o/api
.
The main
method’s comment demonstrates running the class.
The other example Java classes are similar to this one, but call different BlogPostingResource
methods.
See BlogPostingResource for service details.
Below are examples of calling other BlogPosting
REST services using cURL and Java.
Get Blog Posts from Site
You can list a site’s blog posts by executing the following cURL or Java command. As above, replace 1234
with your site’s ID.
BlogPostings_GET_FromSites.sh
Command:
Code:
BlogPostings_GET_FromSites.java
Command:
Code:
The GET method returns the site’s BlogPosting
objects in JSON format.
Get a Blog Post
Get a specific blog post with the following cURL or Java command. Replace 1234
with the blog post’s ID.
Use BlogPostings_GET_FromSites.[java|sh]
to get BlogPosting
IDs.
BlogPostings_GET_ById.sh
Command:
Code:
BlogPostings_GET_ById.java
Command:
Code:
The GET method returns the BlogPosting
fields in JSON format.
Patch a Blog Post
Do a partial edit of an existing blog post with the following cURL and Java commands. Note: replace 1234
with your blog post’s ID.
BlogPostings_PATCH_ById.sh
Command:
Code:
BlogPostings_PATCH_ById.java
Command:
Code:
In this example the article body content is changed from Foo to Bar.
Put a Blog Post
Do a complete overwrite of an existing blog post with the following cURL and Java commands. Note, replace 1234
with your blog post’s ID.
BlogPostings_PUT_ById.sh
Command:
Code:
BlogPostings_PUT_ById.java
Command:
Code:
Delete a Blog Post
Delete an existing blog post with the following cURL and Java commands. Note, replace 1234
with your blog post’s ID.
BlogPostings_DELETE_ById.sh
Command:
Code:
BlogPostings_DELETE_ById.java
Command
Code:
Blog Post Image Services
The cURL commands and Java classes for blog images works in the same way as blog posts.
Files | Description |
---|---|
BlogPostingImages_DELETE_ById.[java\|sh] | Deletes a blog post image by ID. |
BlogPostingImages_GET_ById.[java\|sh] | Get a specific blog post image by ID. |
BlogPostingImages_POST_ToSites.[java\|sh] | Post a blog post image to a site. |
BlogPostingImages_GET_FromSites.[java\|sh] | Get a list of blog post images from a site. |
The API Explorer lists all of the BlogPosting
and BlogPostingImage
services and schemas, and has an interface to try out each service.