Skip to content Skip to sidebar Skip to footer

Outputtng Html By Using Ballerina

Is there any way to output a html page with some data in it by using Ballerina language? Assume that I need the 'orderid' string in the below code to be displayed inside a H1 tag i

Solution 1:

Ballerina is an Integration Language and hence does not support such functionality. However, as an alternative, you can try something as below.

import ballerina.net.http;
import ballerina.lang.messages;
import ballerina.lang.xmls;

@http:BasePath {value:"/shop"}
service echo {

    @http:GET{}
    @http:Path {value:"/order"}
    resource echoGet (message m,@http:QueryParam {value:"orderid"}string orderid) {
        xml xmlPayload = xmls:parse("<html><h1>" + orderid + "</h1></html>");
        messages:setXmlPayload(m, xmlPayload);
        reply m;
    }
}

This constructs an HTML and outputs a reply as below:

<html>
    <h1>123</h1>
</html>

This is still not HTML, rather an HTML constructed as an XML. If you need to render in a webpage, you can extract XML payload directly to be used in your rendering.

Please note that the XML support in Ballerina is under redesign at the moment to provide better native like support. Hence, the above example implementation may change in future.


Post a Comment for "Outputtng Html By Using Ballerina"