Request
object encapsulates incoming HTTP requests to your Alchemy app. It provides a variety of ways to interact with each request’s content, queries, files, and more. We will discuss a few of the most important methods below.
Request
object is passed to the handlers of each route you define on your app.
Request
instance contains an instance of Foundation’s URLComponents
as well as an HTTPMethod
, urlComponents
and method
, respectively. It also contains a few functions and properties for shorthand access to other common values.
url
field.
path
will be /users/2
. Note that path will not include any query from the url.
:
. To quickly access any path parameters on a request, you may use the parameter()
function.
LosslessStringConvertible
(a type that’s initializable from a String
). This overload of the function has a throwing interface, and will throw if the parameter doesn’t exist or isn’t convertible.
parameters
property. Each Parameter
has a key
and value
, representing the path placeholder and the actual component string, respectively.
method
property.
header()
function.
header()
function returns the first matching header value, if one exists. If you’d like to access multiple values of the same header, you may use the headers
parameter, an instance of HTTPHeaders
.
query()
function.
remoteAddress
field, an instance of swift-nio’s SocketAddress?
.
String
, use ip
.
request.body
property, but Alchemy includes a few higher level interfaces for interacting with the request content.
Content-Type
header. Out of the box, Alchemy lets you decode Swift Codable
types from requests encoded by multipart/form-data
, application/x-www-form-urlencoded
, and application/json
.
decode()
function automatically detects the right decoder to use based on the Content-Type
header, but you may pass it a specific decoder if you’d like to specify additional options.
ContentDecoder
and pass it to the decode()
function.
Content
API, which seamlessly integrates a dictionary-like lookup style and Swift’s Codable
.
content()
function or through subscripting directly on Request
. Subscripts can be either String
s for fields or Int
s for array index access.
exists
property.
null
can be checked for using the isNull
property.
string
, int
, bool
, double
, and array
properties. Each has a throwing version as well.
Content
with the typesafe interface of Codable
by calling decode()
on content at the given path.
*
operator to “flatten” it, often called “dot” notation. This returns an array of Content
and subsequent subscripts will be applied to each element.
file()
method, or by using the file
/fileThrowing
properties on Content
. These return an instance of type File
which provides a variety of utilities for interacting with the file.
Content-Type
header, you may access it with the contentType
property.
store()
function to do so easily. It accepts an optional directory to put the file in, as well as an optional id of the filesystem to store in. By default, it will be stored in your default filesystem.