import ballerina/http;
import ballerina/log;type Student record {
    string Name;
    int Grade;
    map<any> Marks;
};@http:ServiceConfig{}
service hello on new http:Listener(9090) {
    @http:ResourceConfig {
        methods: ["POST"],
        body: "orderDetails"
    }
    resource function bindJson(http:Caller caller, http:Request req,
                               json orderDetails) {
        var details = orderDetails.Details;
        http:Response res = new;
        if (details is json) {
            res.setPayload(<@untainted> details);
        } else {
            res.statusCode = 400;
            res.setPayload("Order Details unavailable");
        }
        var result = caller->respond(res);
        if (result is error) {
           log:printError(result.reason(), err = result);
        }
    }
    @http:ResourceConfig {
        methods: ["POST"],
        body: "store",
        consumes: ["application/xml"]
    }
    resource function bindXML(http:Caller caller, http:Request req, xml store) {
        xml city = store.selectDescendants("{http://www.test.com}city");
        http:Response res = new;
        res.setPayload(<@untainted> city);        var result = caller->respond(res);
        if (result is error) {
           log:printError(result.reason(), err = result);
        }
    }
    @http:ResourceConfig {
        methods: ["POST"],
        body: "student",
        consumes: ["application/json"]
    }
    resource function bindStruct(http:Caller caller, http:Request req,
                                 Student student) {
        string name = <@untainted> student.Name;
        int grade = <@untainted> student.Grade;
        string english = <@untained> <string> student.Marks["English"];
        http:Response res = new;
        res.setPayload({ Name: name, Grade: grade, English: english });        var result = caller->respond(res);
        if (result is error) {
           log:printError(result.reason(), err = result);
        }
    }
}

Data Binding

HTTP data binding helps access payload through the last resource signature parameter. The parameter name should be declared in the resource config under the body annotation. string, json, xml, byte[], record, and record[] are supported as parameter types.

import ballerina/http;
import ballerina/log;
type Student record {
    string Name;
    int Grade;
    map<any> Marks;
};
@http:ServiceConfig{}
service hello on new http:Listener(9090) {
    @http:ResourceConfig {
        methods: ["POST"],
        body: "orderDetails"
    }
    resource function bindJson(http:Caller caller, http:Request req,
                               json orderDetails) {

The body annotation represents the entity body of the inbound request.

        var details = orderDetails.Details;
        http:Response res = new;
        if (details is json) {
            res.setPayload(<@untainted> details);
        } else {
            res.statusCode = 400;
            res.setPayload("Order Details unavailable");
        }
        var result = caller->respond(res);
        if (result is error) {
           log:printError(result.reason(), err = result);
        }
    }

Accesses the JSON field values.

    @http:ResourceConfig {
        methods: ["POST"],
        body: "store",
        consumes: ["application/xml"]
    }
    resource function bindXML(http:Caller caller, http:Request req, xml store) {

Binds the XML payload of the inbound request to the store variable.

        xml city = store.selectDescendants("{http://www.test.com}city");
        http:Response res = new;
        res.setPayload(<@untainted> city);

Accesses the XML content.

        var result = caller->respond(res);
        if (result is error) {
           log:printError(result.reason(), err = result);
        }
    }
    @http:ResourceConfig {
        methods: ["POST"],
        body: "student",
        consumes: ["application/json"]
    }
    resource function bindStruct(http:Caller caller, http:Request req,
                                 Student student) {

Binds the JSON payload to a custom record. The payload’s content should match the record.

        string name = <@untainted> student.Name;
        int grade = <@untainted> student.Grade;
        string english = <@untained> <string> student.Marks["English"];
        http:Response res = new;
        res.setPayload({ Name: name, Grade: grade, English: english });

Accesses the fields of the Student record.

        var result = caller->respond(res);
        if (result is error) {
           log:printError(result.reason(), err = result);
        }
    }
}
# To start the service, navigate to the directory that contains the
# `.bal` file and use the `ballerina run` command.
$ ballerina run http_data_binding.bal
#Service deployment
[ballerina/http] started HTTP/WS listener 0.0.0.0:9090
# To invoke the `bindJson` resource, execute the below HTTP request.
$ curl -v http://localhost:9090/hello/bindJson -d '{ "Details": { "ID": "77999", "Name": "XYZ"} , "Location": { "No": "01", "City": "Colombo"}}' -H "Content-Type:application/json"
# Server response:
{"ID":"77999", "Name":"XYZ"}
# To invoke the `bindXML` resource, execute the below HTTP request.
$ curl -v http://localhost:9090/hello/bindXML -d "<h:Store id = \"AST\" xmlns:h=\"http://www.test.com\"><h:street>Main</h:street><h:city>94</h:city></h:Store>" -H "Content-Type:application/xml"
# Server response:
<h:city xmlns:h="http://www.test.com">94</h:city>
# To invoke the `bindStruct` resource, execute the below HTTP request.
$ curl -v http://localhost:9090/hello/bindStruct -d '{ "Name": "John", "Grade": 12, "Marks": {"English" : "85", "IT" : "100"}}' -H "Content-Type:application/json"
# Server response:
{"Name":"John", "Grade":12, "English":"85"}