Back to Examples
- Binding patterns
- Typed binding pattern
- Wildcard binding pattern
- List binding patterns
- Rest binding pattern in list binding pattern
- Mapping binding pattern
- Rest binding pattern in mapping binding pattern
- Error binding pattern
- Rest binding pattern in error binding pattern
- Single use of typed binding patterns
- Single use of typed binding patterns with on fail clause
- Iterative use of typed binding patterns
- List binding pattern in match statement
- Mapping binding pattern in match statement
- Error binding pattern in match statement
- Query expressions
- Sort iterable objects
- Let clause
- Limit clause
- Join iterable objects
- Outer Join clause
- Query tables
- Create tables with a query
- Create maps with a query
- Create streams with a query
- On conflict clause
- Advanced conflict handling
- Iterate over XML with a query
- Nested query expressions
- Destructure records using a query
- Querying streams
- Aggregation
- JSON type
- Access JSON elements
- Access optional JSON elements
- Match statement with maps
- Convert from user-defined type to JSON
- Convert from table and XML to JSON
- Convert from JSON to user-defined type
- Cast JSON to user-defined type
- Resource method typing
- JSON numbers
- JSON to record
- JSON to record with projection
- JSONPath expressions
- Asynchronous function calls
- Named workers
- Sequence diagrams
- Wait for workers
- Strands
- Named worker return values
- Alternate wait
- Multiple wait
- Named workers and futures
- Inter-worker message passing
- Alternate receive
- Multiple receive
- Conditional send
- Inter-worker failure propagation
- Named worker with on fail clause
- Synchronize message passing
- Asynchronize message passing
- Flush
- Fork
HTTP service - Cookies
HTTP cookies can track, personalize, and manage the session in the service. The cookies can be accessed from the getCookies
method of the http:Request
. Setting cookies back in the response is done by the addCookie
method of the http:Response
. This is useful for services to maintain the state.
import ballerina/http;
listener http:Listener serverEP = new (9095);
service /cookieDemo on serverEP {
resource function post login(map<json> details) returns http:Response|http:Unauthorized|error {
// Retrieve the username and password.
json name = check details.name;
json password = check details.password;
// Check the password value.
if password == "p@ssw0rd" {
// Create a new cookie by setting `name` as the `username` and `value` as the logged-in user's name.
// Set the cookies path as `/` to apply it to all the resources in the service.
http:Cookie cookie = new ("username", name.toString(), path = "/");
http:Response response = new;
// Add the created cookie to the response.
response.addCookie(cookie);
// Set a message payload to inform that the login has
// been succeeded.
response.setTextPayload("Login succeeded");
return response;
}
return http:UNAUTHORIZED;
}
resource function get welcome(http:Request req) returns string|http:NotFound {
// Retrieve cookies from the request.
http:Cookie[] cookies = req.getCookies();
// Get the cookie value of the `username`.
http:Cookie[] usernameCookie = cookies.filter(function(http:Cookie cookie) returns boolean {
return cookie.name == "username";
});
if usernameCookie.length() > 0 {
string? user = usernameCookie[0].value;
if user is string {
// Respond with the username added to the welcome message.
return "Welcome back " + user;
}
}
return http:NOT_FOUND;
}
}
Run the service as follows.
$ bal run cookie_server.bal
Tip: You can invoke the above service via the Cookies client example.
Related links
PreviousAccess logs
NextChunking