import ballerina/http;
type PersonAccount record {
string name;
int accountNo;
};
service /bank on new http:Listener(9090) {
// The resource returns the json type values and the `Content-type` header is set according to the `mediaType`
// field of [@http:Payload](https://docs.central.ballerina.io/ballerina/http/latest/records/HttpPayload)
// annotation.
resource function get branch() returns
@http:Payload {mediaType:"application/json+id"} json {
return { branch : ["Colombo, Srilanka"]};
}
// The [StatusCodeResponse](https://docs.central.ballerina.io/ballerina/http/latest/types#StatusCodeResponse)
// can be state as return type to send responses with specific HTTP status codes.
resource function get [string 'type]()
returns http:Ok|http:InternalServerError {
if 'type == "open" {
// Creates response with 200 status code and set body as response payload.
http:Ok ok = {body: "Bank is open"};
return ok;
} else {
// Creates response with 500 status code and set body as response payload.
http:InternalServerError err = {body: "Bank is closed"};
return err;
}
}
// Inline response records are useful to return headers and body along with status code. In this instance the
// return type is a subtype of [http:Created](https://docs.central.ballerina.io/ballerina/http/latest/records/Created)
// record, hence 201 response will be sent.
resource function put account(@http:Payload string name)
returns record {|*http:Created; PersonAccount body;|} {
PersonAccount account = {accountNo: 84230, name: name};
return {
mediaType: "application/account+json",
headers: {
"Location": "/myServer/084230"
},
body: account
};
}
}
Typed resource responsesThe resource method can return |
import ballerina/http;
type PersonAccount record {
string name;
int accountNo;
};
service /bank on new http:Listener(9090) {
resource function get branch() returns
@http:Payload {mediaType:"application/json+id"} json {
return { branch : ["Colombo, Srilanka"]};
}
The resource returns the json type values and the Content-type
header is set according to the mediaType
field of @http:Payload
annotation.
resource function get [string 'type]()
returns http:Ok|http:InternalServerError {
if 'type == "open" {
The StatusCodeResponse can be state as return type to send responses with specific HTTP status codes.
http:Ok ok = {body: "Bank is open"};
return ok;
} else {
Creates response with 200 status code and set body as response payload.
http:InternalServerError err = {body: "Bank is closed"};
return err;
}
}
Creates response with 500 status code and set body as response payload.
resource function put account(@http:Payload string name)
returns record {|*http:Created; PersonAccount body;|} {
PersonAccount account = {accountNo: 84230, name: name};
return {
mediaType: "application/account+json",
headers: {
"Location": "/myServer/084230"
},
body: account
};
}
}
Inline response records are useful to return headers and body along with status code. In this instance the return type is a subtype of http:Created record, hence 201 response will be sent.
bal run http_resource_returns.bal
Run the cURL command below to invoke bank/branch resource.
curl -v "http://localhost:9090/bank/branch"
> GET /bank/branch HTTP/1.1
> Host: localhost:9090
> User-Agent: curl/7.64.1
> Accept: */*
>
< HTTP/1.1 200 OK
< content-type: application/json+id
< content-length: 32
< server: ballerina
< date: Sat, 15 May 2021 16:14:10 +0530
<
* Connection #0 to host localhost left intact
{"branch":["Colombo, Srilanka"]}* Closing connection 0
Run the cURL command below to invoke second resource.
curl -v "http://localhost:9090/bank/open"
> GET /bank/open HTTP/1.1
> Host: localhost:9090
> User-Agent: curl/7.64.1
> Accept: */*
>
< HTTP/1.1 200 OK
< content-type: text/plain
< content-length: 12
< server: ballerina
< date: Sat, 15 May 2021 16:16:56 +0530
<
* Connection #0 to host localhost left intact
Bank is open* Closing connection 0
Run the cURL command below to invoke bank/account resource.
curl -v "http://localhost:9090/bank/account" -d "bal" -X PUT
> PUT /bank/account HTTP/1.1
> Host: localhost:9090
> User-Agent: curl/7.64.1
> Accept: */*
> Content-Length: 3
> Content-Type: application/x-www-form-urlencoded
>
< HTTP/1.1 201 Created
< Location: /myServer/084230
< content-type: application/account+json
< content-length: 33
< server: ballerina
< date: Sat, 15 May 2021 16:19:31 +0530
<
* Connection #0 to host localhost left intact
{"name":"bal", "accountNo":84230}* Closing connection 0