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
- 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
- Conditional send
- Inter-worker failure propagation
- Named worker with on fail clause
- Synchronize message passing
- Asynchronize message passing
- Flush
- Fork
REST service - Send different status codes
The subtypes of the http:StatusCodeResponse
record type represent different HTTP status code responses. Returning them from the resource function results in the relevant HTTP status code response. To send a non-entity body response, use the relevant constant value declared in the http
module. These constant values can be directly returned from the resource method by specifying the relevant return type in the resource function signature. Use this when different status code responses need to be sent without a body and headers.
import ballerina/http;
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 resource returns the `409 Conflict` status code as the error response status code using
// the `StatusCodeResponse` constants. This constant does not have a body or headers.
resource function post albums(Album album) returns Album|http:Conflict {
if albums.hasKey(album.title) {
return http:CONFLICT;
}
albums.add(album);
return album;
}
}
Run the service as follows.
$ bal run send_different_status_code.bal
Invoke the service by executing the following cURL command in a new terminal.
$ curl http://localhost:9090/albums -H "Content-type:application/json" -d "{\"title\": \"Blue Train\", \"artist\": \"John Coltrane\"}" -v> POST /albums HTTP/1.1> Host: localhost:9090> User-Agent: curl/7.79.1> Accept: */*> Content-type:application/json> Content-Length: 50> * Mark bundle as not supporting multiuse< HTTP/1.1 409 Conflict< content-length: 0< server: ballerina< date: Mon, 5 Dec 2022 16:23:51 +0530<
Tip: You can invoke the above service via the Send request/Receive response client example.
Related links
PreviousSend response
NextSend different status codes with payload