Renders the body content of a message from a template. The template can produce plain text, Json or XML. Variables in the template are substituted with values from the body, header, query parameters, etc. If the extension of a referenced template file is
.xml it will use
XMLTemplateEngine otherwise
StreamingTemplateEngine . Have a look at the samples in
examples/templating . When the
contentType is a JSON variant (e.g.,
application/json), the engine automatically escapes all inserted values. For example, in the
JSON templating example , executing
curl "localhost:2000/?answer=20" returns
{ "answer" : "20" }. The quotes surrounding the value 20 are added by the auto-escaping mechanism to ensure the output remains a valid string. This feature significantly mitigates security risks by preventing inadvertent JSON injection attacks.
Copy api : port : 2000 flow : - request : - template : contentType : application/json src : | { "name": ${params.name}, "age": ${params.age} } - response : - template : location : template.xml - return : { }
api:
port: 2000
flow:
- request:
- template:
contentType: application/json
src: |
{
"name": ${params.name},
"age": ${params.age}
}
- response:
- template:
location: template.xml
- return: {}
Syntax Copy template : charset : <string> contentType : <string> location : <string> pretty : <boolean> src : <string>
template:
charset: <string>
contentType: <string>
location: <string>
pretty: <boolean>
src: <string>
Sample Copy < api port = " 2000" > < request> < template contentType = " text/plain" > Hello ${params.name}!</ template> </ request> < return statusCode = " 200" /> </ api> < api name = " JSON" port = " 2000" method = " GET" > < request> < template contentType = " application/json" pretty = " yes" > { "answer": ${params.answer}, "foo": 7 } </ template> </ request> < return statusCode = " 200" /> </ api> < api port = " 2000" > < request> < template location = " template.xml" /> </ request> < return statusCode = " 200" /> </ api> < destinations xmlns: gsp= ' http://groovy.codehaus.org/2005/gsp' > < gsp: scriptlet> def answer = 42;</ gsp: scriptlet> < answer> < gsp: expression> answer</ gsp: expression> </ answer> < gsp: scriptlet> import groovy.xml.XmlSlurper def xml = new XmlSlurper().parseText(body) </ gsp: scriptlet> < gsp: scriptlet> xml.children().each { </ gsp: scriptlet> < destination> < gsp: expression> it</ gsp: expression> </ destination> < gsp: scriptlet> } </ gsp: scriptlet> </ destinations>
<api port="2000">
<request>
<template contentType="text/plain">Hello ${params.name}!</template>
</request>
<return statusCode="200"/>
</api><api name="JSON" port="2000" method="GET">
<request>
<template contentType="application/json" pretty="yes">
{
"answer": ${params.answer},
"foo": 7
}
</template>
</request>
<return statusCode="200"/>
</api><api port="2000">
<request>
<template location="template.xml"/>
</request>
<return statusCode="200"/>
</api><destinations xmlns:gsp='http://groovy.codehaus.org/2005/gsp'>
<gsp:scriptlet>def answer = 42;</gsp:scriptlet>
<answer><gsp:expression>answer</gsp:expression></answer>
<gsp:scriptlet>
import groovy.xml.XmlSlurper
def xml = new XmlSlurper().parseText(body)
</gsp:scriptlet>
<gsp:scriptlet>xml.children().each { </gsp:scriptlet>
<destination><gsp:expression>it</gsp:expression></destination>
<gsp:scriptlet> } </gsp:scriptlet>
</destinations>
Attributes Name Required Default Description Examples charset false UTF-8 Encoding of the template text. UTF-16, iso-8859-1 contentType false text/plain Content-Type of the generated body content. application/json location false N/A A file or URL location where the content that should be set as body could be found conf/body.txt pretty false false Format the content of the template. Depending on the contentType the a formatter for JSON, XML or plain text is used. true src false - - -
Can be used in