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
REST service - Header parameter
The @http:header
annotation allows reading header values from the request. The annotation can be used to annotate a given resource parameter. The name of the parameter must match the name of the header. If there is a mismatch, then the header name must be given in the annotation configuration. The resource parameter can be a simple type or an array type (i.e., string version
or string[] versions
). If there are many headers to read, a record type can be used as the parameter. Unless the parameter is optional (i.e., string? version
), a 400 Bad Request
response is sent to the client in the absence of the mapping header.
import ballerina/http;
import ballerina/mime;
type Album readonly & record {|
string title;
string artist;
|};
table<Album> key(title) albums = table [
{title: "Blue Train", artist: "John Coltrane"},
{title: "Jeru", artist: "Gerry Mulligan"}
];
service / on new http:Listener(9090) {
// The `accept` argument with `@http:Header` annotation takes the value of the `Accept` request header.
resource function get albums(@http:Header string accept) returns Album[]|http:NotAcceptable {
if !string:equalsIgnoreCaseAscii(accept, mime:APPLICATION_JSON) {
return http:NOT_ACCEPTABLE;
}
return albums.toArray();
}
}
Run the service as follows.
$ bal run http_headers.bal
Invoke the service by executing the following cURL command in a new terminal.
$ curl "http://localhost:9090/albums" -H "Accept:application/json"[{"title":"Blue Train", "artist":"John Coltrane"}, {"title":"Jeru", "artist":"Gerry Mulligan"}]
Tip: You can invoke the above service via the client given in the HTTP client - Header parameter example.