Lesson 8
HTTP bindings
HTTP bindings
Protocol details are traits, not part of an operation’s structure. The same
GetCity operation can be served over different protocols; @http is how you
say “and over REST, it looks like this”.
@readonly
@http(method: "GET", uri: "/cities/{cityId}", code: 200)
operation GetCity {
input := {
@required
@httpLabel
cityId: String
@httpQuery("verbose")
verbose: Boolean
}
output := {
@required
name: String
}
}
@http takes three things: the HTTP method, the uri pattern, and the success
status code.
Where does each member go?
By default, input members land in the JSON request body. Binding traits move them elsewhere:
@httpLabel— fills a{placeholder}in the URI. Every{placeholder}must match a@required @httpLabelmember.@httpQuery("name")— becomes a query-string parameter.@httpHeader("Name")— becomes a request header.
Anything not bound stays in the body.
Your turn
Add the HTTP layer to GetCity. The hasTrait check asserts the exact
@http value, so the method, URI, and code all have to match. And because the
URI has a {cityId} label, cityId must be a @required @httpLabel member or
the model won’t compile.