Get request information, such as headers, cookies, and so on.
Get route parameter values.
Get parameters of a query string.
Receive body contents, for example, data objects, form parameters, and files.
General request information
Inside a route handler, you can get access to a request using the call.request property. This returns the ApplicationRequest instance and provides access to various request parameters. For example, the code snippet below shows how get a request URI:
The ApplicationRequest object allows you to get access to various request data, for example:
To access all request headers, use the ApplicationRequest.headers property. You can also get access to specific headers using dedicated extension functions, such as
cacheControl, and so on.
Use the ApplicationRequest.local property to get access to connection details such as a host name, port, scheme, and so on.
When handling requests, you can get access to route parameter values using the
call.parameters property. For example,
call.parameters["login"] in the code snippet below will return admin for the
To get access to parameters of a query string, you can use the ApplicationRequest.queryParameters property. For example, if a request is made to
/products?price=asc, you can access the
price query parameter in this way:
You can also obtain the entire query string using the ApplicationRequest.queryString function.
This section shows how to receive body contents sent with
Ktor provides a ContentNegotiation feature to negotiate the media type of request and deserialize content to an object of a required type. To receive and convert content for a request, call the receive method that accepts a data class as a parameter:
You can learn more from Content negotiation and serialization.
Ktor allows you to receive form parameters sent with both
multipart/form-data types using the receiveParameters function. The example below shows an HTTP client
POST request with parameters passed in a body:
You can obtain the
first_name parameter value in code as follows:
firstName variable will be initialized with Jet.
Multipart form data
If you need to receive a file sent as a part of a multipart request, call the receiveMultipart function and then loop over each part as required. In the example below,
PartData.FileItem is used to receive a file as a byte stream.
Learn how to run this sample from upload-file.
If you need to access the raw body payload and parse it by yourself, you can use the following functions: