Back

Delivering HTML, markdown or raw text via API

Description

This is an example of how you can call the Scripting API to deliver the body field of a content field in three ways, as HTML, as rendered Markdown and as text stripped of HTML. You can pass it a content id (type:generic content) like this https://demo.dotcms.com/api/vtl/dynamic/id/{myId} and it will find the content with the id and return the content.body field as plain text, as HTML and as rendered Markdown.

For more information on what you can do with the Scripting endpoint see the link below:


Link: https://dotcms.com/docs/latest/scripting-api#scripting-apidocumentation-for-the-dotcms-content-management

Code

curl -u admin@dotcms.com:admin -XPOST https://demo.dotcms.com/api/vtl/dynamic/id/735c44a5-3e6b-4047-a8c3-8ea9146e1762 -H "Content-Type:application/json" -d '{ 
#set($id = $pathParam) 
#if(!$id) 
    #set($id = "f6406747-0220-41fb-86e4-32bce21a8822") 
#end 
#set($myContent = $dotcontent.find($id)) 
$dotJSON.put("identifier", $myContent.identifier) 
#set($myBody = $myContent.body) 
$dotJSON.put("bodyHTML", $myBody) 
$dotJSON.put("bodyMarkdown", $markdown.parse($myBody)
$dotJSON.put("bodyStripped", $myBody.replaceAll("<[^>]*>", "")
$dotJSON.put("title", $myContent.title) }'    

Have you created code you'd like to share with the dotCMS community?

Share your code Become a Contributor